C#Access插入查询不工作
好的,这段代码不会产生错误,但不会将数据添加到数据库中。按下按钮时,应将文本框中的所有值插入数据库C#Access插入查询不工作,c#,visual-studio,ms-access,ms-access-2010,C#,Visual Studio,Ms Access,Ms Access 2010,好的,这段代码不会产生错误,但不会将数据添加到数据库中。按下按钮时,应将文本框中的所有值插入数据库 private void addSportButton_Click(object sender, EventArgs e){ for(int i = 0; i < numberOfPlayers; i++){ OleDbConnection connection = new OleDbConnection(CONNECTION STRING HERE); OleDbC
private void addSportButton_Click(object sender, EventArgs e){
for(int i = 0; i < numberOfPlayers; i++){
OleDbConnection connection = new OleDbConnection(CONNECTION STRING HERE);
OleDbCommand command = new OleDbCommand();
command.CommandText = "INSERT INTO TotalPlayerName ([PlayerName]) VALUES (@name)";
command.CommandType = CommandType.Text;
command.Connection = connection;
connection.Open();
command.Parameters.Add("@name", OleDbType.VarWChar).Value = textBox[i].Text;
command.ExecuteNonQuery();
connection.Close();
}
}
private void addSportButton\u单击(对象发送者,事件参数e){
for(int i=0;i
我做错了什么
编辑:
更改了代码前面部分中的一些内容,现在添加了行,但PlayerName字段中没有显示任何内容
用于创建文本框的代码
for (int t = 0; t < 18; t++)
{
textBox[t] = new TextBox();
this.Controls.Add(textBox[t]);
this.textBox[t].Font = new System.Drawing.Font("Calibri", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
// if it is the first text box then it must go in this location
if (t == 0)
{
textBox[t].Location = new Point(32, 41);
textBox[t].Visible = true;
}
else
{
// every other text box will be 27px below the previous
textBox[t].Location = new System.Drawing.Point(32, 41 + (t * 27));
textBox[t].Visible = false;
}
}
for(int t=0;t<18;t++)
{
textBox[t]=新的textBox();
this.Controls.Add(textBox[t]);
this.textBox[t].Font=new System.Drawing.Font(“Calibri”,9.75F,System.Drawing.FontStyle.Regular,System.Drawing.GraphicsUnit.Point,((字节)(0));
//如果它是第一个文本框,则它必须位于此位置
如果(t==0)
{
文本框[t]。位置=新点(32,41);
文本框[t]。可见=真;
}
其他的
{
//每隔一个文本框将比上一个文本框低27px
textBox[t].Location=new System.Drawing.Point(32,41+(t*27));
文本框[t]。可见=假;
}
}
当插入“失败”且没有错误消息时,十有八九的情况下……您在错误的数据库中查找
private void addSportButton_Click(object sender, EventArgs e){
for(int i = 0; i < numberOfPlayers; i++){
OleDbConnection connection = new OleDbConnection(CONNECTION STRING HERE);
OleDbCommand command = new OleDbCommand();
command.CommandText = "INSERT INTO TotalPlayerName ([PlayerName]) VALUES (@name)";
command.CommandType = CommandType.Text;
command.Connection = connection;
connection.Open();
command.Parameters.Add("@name", OleDbType.VarWChar).Value = textBox[i].Text;
command.ExecuteNonQuery();
connection.Close();
}
}
您正在向数据库“A”插入
但是在数据库“B”中查找记录您确定有错误吗?在
try catch
block中插入代码我应该在每个部分中添加什么?至于参数,每次通过for循环时肯定会有一个参数?因此代码肯定在运行,即numberOfPlayers大于零,您可以在调试器中单步执行?是的,代码工作正常您的项目文件之间是否列出了ACCDB/MDB文件?如果是,单击它并查看属性值复制到输出目录
。它的价值是什么?啊!这正是发生在我身上的事情。在我的例子中,我提供了一个相对路径,它指向bin/debug中的一个mdb文件,当我在主项目文件夹中查找插入的记录时,我抓狂了。令人惊叹的。谢谢你,史蒂夫。