我正在尝试从文本框中向AccessDB插入一些数据。并且代码也成功运行,但是新数据未出现在数据库中所有数据类型或短文本
try
{
OleDbConnection connection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=dsms.accdb");
connection.Open();
string Dcode = textDcode.Text;
string Dname = textDname.Text;
string Units = textDunit.Text;
string Price = textDbuyp.Text;
string BPrice = textDsellp.Text;
string Category = textDcategory.Text;
string my_querry = "INSERT INTO Drugs(Dcode, Dname, Units, Price, BPrice, Category)VALUES('"+Dcode+"','"+Dname+"','"+Units+"','"+Price+"','"+BPrice+"','"+Category+"')";
OleDbCommand cmd = new OleDbCommand(my_querry, connection);
cmd.ExecuteNonQuery();
MessageBox.Show("Data saved successfully");
}
catch (Exception ex)
{
MessageBox.Show("Failed due to " + ex.Message);
}
finally
{
connection.Close();
}
1)用实际值创建插入语句。为此,请在字段中放个休假[]
string Dcode = textDcode.Text; string Dname = textDname.Text; string Units = textDunit.Text; string Price = textDbuyp.Text; string BPrice = textDsellp.Text; string Category = textDcategory.Text;
示例:
INSERT INTO Drugs(Dcode, Dname, Units, Price, BPrice, Category)VALUES('0001','nombre A','3','3454545','2453245','categoria A')
并使用这些值手动建立您的敏锐度。在数据库引擎中单独运行它。
2)确认您已从数据库控制台正确地插入表中。
3)正确插入句子后,请从c#代码中进行设置,然后尝试从c#中插入。示例:
string my_querry = "INSERT INTO Drugs(Dcode, Dname, Units, Price, BPrice, Category)VALUES('0001','nombre A','3','3454545','2453245','categoria A')";
4)当c#的插入有效时,用变量替换字段的值。示例:
string my_querry = String.Format("INSERT INTO Drugs(Dcode, Dname, Units, Price, BPrice, Category)VALUES('{0}','{1}','{2}','{3}','{4}','{5}')",Dname, Units, Price, BPrice, Category) ;