Created
July 6, 2012 03:39
-
-
Save ne-sachirou/3057922 to your computer and use it in GitHub Desktop.
ASP.NET C# and MySQL example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using MySql.Data.MySqlClient; | |
namespace insert | |
{ | |
public class User | |
{ | |
public string ID; | |
public string Name; | |
public string Pass; | |
public int Age; | |
public User(string id, string name, string pass, int age) | |
{ | |
if (id.Length > 5 || name.Length > 20 || pass.Length > 3) | |
throw new FormatException("Illegal Format."); | |
ID = id; | |
Name = name; | |
Pass = pass; | |
Age = age; | |
} | |
} | |
public partial class _Default : System.Web.UI.Page | |
{ | |
private int Insert(User user) | |
{ | |
string connectionInfo = string.Format("server={0};user id={1};password={2};database={3};charset=utf8;", | |
"localhost", "root", "", "wp22"); | |
using (var connection = new MySqlConnection(connectionInfo)) | |
{ | |
connection.Open(); | |
var command = new MySqlCommand("Insert Into user (id, name, pass, age) Values (?ID, ?NAME, ?PASS, ?AGE);", connection); | |
command.Parameters.AddWithValue("?ID", user.ID); | |
command.Parameters.AddWithValue("?NAME", user.Name); | |
command.Parameters.AddWithValue("?PASS", user.Pass); | |
command.Parameters.AddWithValue("?AGE", user.Age); | |
return command.ExecuteNonQuery(); | |
} | |
} | |
protected void Page_Load(object sender, EventArgs e) { } | |
protected void ButtonInsert_Click(object sender, EventArgs e) | |
{ | |
int age; | |
try | |
{ | |
if (!int.TryParse(TextBoxAge.Text, out age)) throw new FormatException("Invalid Age Format."); | |
var user = new User(TextBoxID.Text, TextBoxName.Text, TextBoxPass.Text, age); | |
Insert(user); | |
LabelResult.Text = "OK"; | |
} | |
catch (FormatException err) | |
{ | |
LabelResult.Text = string.Format("Canceled ({0})", err.Message); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Web; | |
using MySql.Data.MySqlClient; | |
namespace DBConnect | |
{ | |
public partial class _Default : System.Web.UI.Page | |
{ | |
protected void Page_Load(object sender, EventArgs e) | |
{ | |
string connectInfo = string.Format("server={0};user id={1};password={2};database={3};", | |
"localhost", "root", "", "wp22"); | |
using (var connection = new MySqlConnection(connectInfo)) | |
{ | |
connection.Open(); | |
var command = new MySqlCommand("Select id, pass, name, age From user;", connection); | |
LabelCommandText.Text = command.CommandText; | |
using (MySqlDataReader reader = command.ExecuteReader()) | |
{ | |
if (reader.HasRows) | |
{ | |
LiteralResult.Text += "<table><tr><th>id</th><th>pass</th><th>name</th><th>age</th></tr>"; | |
while (reader.Read()) | |
{ | |
LiteralResult.Text += string.Format("<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td></tr>", | |
HttpUtility.HtmlEncode(reader.GetString(0)), | |
HttpUtility.HtmlEncode(reader.GetString(1)), | |
HttpUtility.HtmlEncode(reader.GetString(2)), | |
reader.GetInt32(3) | |
); | |
} | |
LiteralResult.Text += "</table>"; | |
} | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using MySql.Data.MySqlClient; | |
namespace kadai03 | |
{ | |
public partial class _Default : System.Web.UI.Page | |
{ | |
private bool Login(string userID, string password) | |
{ | |
string connectInfo = string.Format("server={0};user id={1};password={2};database={3};", | |
"localhost", "root", "", "wp22"); | |
using (var connection = new MySqlConnection(connectInfo)) | |
{ | |
connection.Open(); | |
var command = new MySqlCommand("Select Count(*) From user Where id = ?ID And pass = ?PASS;", connection); | |
command.Parameters.AddWithValue("?ID", userID); | |
command.Parameters.AddWithValue("?PASS", password); | |
if (Convert.ToInt32(command.ExecuteScalar()) != 0) return true; | |
} | |
return false; | |
} | |
protected void Page_Load(object sender, EventArgs e) { } | |
protected void ButtonLogin_Click(object sender, EventArgs e) | |
{ | |
bool isCorrectUser = Login(TextBoxID.Text, TextBoxPassword.Text); | |
if (isCorrectUser) LabelResult.Text = "ログインに成功しました。"; | |
else LabelResult.Text = "ログインに失敗しました。"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
THat's look good, i will try it after