Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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);
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment