[英]How I can Insert Data in the MySQL Database?
I have a ASP.NET Application and a MySQL Database. 我有一个ASP.NET应用程序和一个MySQL数据库。 I want write a Class to insert,delete and show the Data from the database.
我想编写一个类来插入,删除和显示数据库中的数据。 I have a Connection to the Database but I can't insert data in the database.
我有一个数据库连接,但我无法在数据库中插入数据。
My Class insert method: 我的类插入方法:
public string CreateEntry(string Connectionstring, string mitarbeiter)
{
connection = new MySqlConnection(Connectionstring);
try
{
var command = connection.CreateCommand();
command.CommandText = "INSERT INTO tb_mitarbeiter (Vorname) VALUES ('tom')";
connection.Open();
return "Mitarbeiter wurde angelegt";
}
catch (Exception ex)
{
return ex.Message;
}
finally
{
connection.Close();
}
}
The Connectionstring
is correct. Connectionstring
是正确的。 I don't get a error but there is no data in the database. 我没有收到错误,但数据库中没有数据。
My tablename: tb_mitarbeiter columns: ID and Vorname 我的tablename:tb_mitarbeiter列:ID和Vorname
You should simply execute the command 您只需执行该命令即可
....
MySqlCommand command = connection.CreateCommand();
command.CommandText = "INSERT INTO tb_mitarbeiter (Vorname) VALUES ('tom')";
connection.Open();
command.ExecuteNonQuery();
....
I suppose that mitarbeiter
is the real value that should be set in the database. 我想
mitarbeiter
是应该在数据库中设置的真正值。
If this is the case remember to use parameters to insert/update your data 如果是这种情况,请记住使用参数来插入/更新数据
MySqlCommand command = connection.CreateCommand();
command.CommandText = "INSERT INTO tb_mitarbeiter (Vorname) VALUES (?name)";
command.Parameters.AddWithValue("?name", mitarbeiter);
connection.Open();
command.ExecuteNonQuery();
To do a Insert / Update / Delete u should add 要执行插入/更新/删除,您应该添加
connection.Open();
command.ExecuteNonQuery();
For select ()to show data from database use: 对于select()来显示数据库使用的数据:
connection.Open();
command.ExecuteReader();
You forgot to execute the command by calling command.ExecuteNonQuery(). 您忘记通过调用command.ExecuteNonQuery()来执行该命令。 This is how I would typically do it:
这就是我通常会这样做的方式:
public string CreateEntry(string connectionString, string valueToInsert)
{
var stringToReturn = "";
try
{
using(var connection = new MySqlConnection(connectionString))
{
//Open connection
connection.Open();
//Compose query using sql parameters
var sqlCommand = "INSERT INTO table_name (field_name) VALUES (@valueToInsert)";
//Create mysql command and pass sql query
using(var command = new MySqlCommand(sqlCommand, connection))
{
command.Parameters.AddWithValue("@valueToInsert", valueToInsert);
command.ExecuteNonQuery();
}
stringToReturn ="Success Message";
}
}
catch(exception ex)
{
stringToReturn = "Error Message: " + ex.Message;
}
return stringToReturn;
}
There are a few key things to keep in mind: 要记住以下几个关键事项:
You are not executing the command use SqlCommand.ExecuteNonQuery 您没有执行命令使用SqlCommand.ExecuteNonQuery
try
{
MySqlCommand command = connection.CreateCommand();
command.CommandText = "INSERT INTO tb_mitarbeiter (Vorname) VALUES ('tom')";
connection.Open();
command.ExecuteNonQuery();
return "Mitarbeiter wurde angelegt";
}
catch (Exception ex)
{
return ex.Message;
}
finally
{
connection.Close();
}
You missed to write this:- 你错过了写这个: -
....
connection.Open();
command.ExecuteNonQuery();
....
{ {
string MyConnection2 = "datasource=localhost;port=3306;username=root;password=1234"; string MyConnection2 =“datasource = localhost; port = 3306; username = root; password = 1234”;
string Query = "insert into DBname.TableName(id,Name,First_Name,Age,Address) values('" +this.IdTextBox.Text+ "','" +this.NameTextBox.Text+ "','" +this.FirstnameTextBox.Text+ "','" +this.AgeTextBox.Text+ "','" +this.AddressTextBox.Text+ "');";
MySqlConnection MyConn2 = new MySqlConnection(MyConnection2);
MySqlCommand MyCommand2 = new MySqlCommand(Query, MyConn2);
MySqlDataReader MyReader2;
MyConn2.Open();
MyReader2 = MyCommand2.ExecuteReader();
MessageBox.Show("Save Data");
while (MyReader2.Read())
{
}
MyConn2.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
} }
You can also used Sql parameter to prevent Sql Injection 您还可以使用Sql参数来阻止Sql注入
try
{
MySqlCommand command = connection.CreateCommand();
command.CommandText = @"INSERT INTO `tb_mitarbeiter` (`Vorname`) VALUES (@tom)";
command.Parameters.AddWithValue("@tom", tom);
connection.Open();
command.ExecuteNonQuery();
return "Mitarbeiter wurde angelegt";
}
catch (Exception ex)
{
return ex.Message;
}
finally
{
command.Dispose();
command.Close();
connection.Close();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.