我正在使用 c# 并将数据插入 Access 数据库。它运行正常,但当我尝试插入数据时崩溃,知道为什么吗?
public partial class StudentInfo : Form
{
private OleDbConnection myCon;
public StudentInfo()
{
InitializeComponent();
myCon = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\database program\database program\Students.mdb");
}
private void InsertBtn_Click(object sender, EventArgs e)
{
OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Insert into StudentInfo(Rollno,SName,SFather,SAdress) Values ('"+ Rollnotb.Text+"','"+nametb.Text+"','"+fathertb.Text+"','"+adresstb.Text+"')";
cmd.Connection=myCon;
myCon.Open();
cmd.ExecuteNonQuery();
myCon.Close();
}
}
最佳答案
问题可能是您将所有字段作为文本
值插入,并且可能有些字段在 MS Access 表中定义为数字(rollNo
?)
此外,习惯在查询中使用参数:
cmd.CommandText = "Insert into StudentInfo(Rollno,SName,SFather,SAdress) Values (?,?,?,?)";
cmd.Parameters.Add(new OleDbParameter("@rollNo", rollNoValue)); //etc.
关于c# - 将数据插入 Access 数据库时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6556184/