这是我的插入方法:
public void Insert(string table, string column, string value)
{
//Insert values into the database.
//Example: INSERT INTO names (name, age) VALUES('John Smith', '33')
//Code: MySQLClient.Insert("names", "name, age", "'John Smith, '33'");
string query = "INSERT INTO " + table + " (" + column + ") VALUES (" + value + ")";
try
{
if (this.Open())
{
//Opens a connection, if succefull; run the query and then close the connection.
MySqlCommand cmd = new MySqlCommand(query, conn);
cmd.ExecuteNonQuery();
this.Close();
}
}
catch { }
return;
}
然后这是我的按钮单击,它应该实际添加用户:
private void createUser_Click_1(object sender, EventArgs e)
{
//Example: INSERT INTO names (name, age) VALUES('John Smith', '33')
//Code: MySQLClient.Insert("names", "name, age", "'John Smith, '33'");
//gets the next userid to assign to the new user
int counter = sqlClient.Count("UserList") + 1;
//testing just to make sure values are correct
User user1 = new User(counter, textEmail.Text, textPass.Text, textLNAME.Text, textFNAME.Text);
currentUser.AppendText(user1.ToString());
//This works to add a user manually to the table
//This is what I want to automate
sqlClient.Insert("UserList", "userid, email, password, lastname, firstname", "counter, textEmail.Text, textPass.Text, textLNAME.Text, textFNAME.Text");
//just to let the user know it worked
reaction.Text = "Success!";
}
可能有一些我以前从未听说过或使用过的方法,我只是想念。我知道 insert 方法正在寻找要插入到我的数据库表中的字符串。我有一系列文本框供用户输入他们的信息,然后我想将这些字符串发送到数据库。如何在程序中将这些文本框值转换为字符串?请原谅,我对此很陌生。