Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将此表单的值发送到accdb时_C# - Fatal编程技术网

C# 将此表单的值发送到accdb时

C# 将此表单的值发送到accdb时,c#,C#,我使用了这个代码脚本,但没有错误,但数据库没有得到更新,请帮助 无线电btn1、2、3是[年(二、三、四)] 和btn 4,5电台属于另一组[无候选人(1,2)] chk盒4,5,6,7,8,9属于另一组。对于[活动(论文展示、测验、调试等)] 这张表格是我所在部门的研讨会登记表。 请帮忙 编码: OleDbCommand command = new OleDbCommand("INSERT INTO Registration(Name,Branch,College,Contact)VALUES

我使用了这个代码脚本,但没有错误,但数据库没有得到更新,请帮助 无线电btn1、2、3是[年(二、三、四)] 和btn 4,5电台属于另一组[无候选人(1,2)] chk盒4,5,6,7,8,9属于另一组。对于[活动(论文展示、测验、调试等)] 这张表格是我所在部门的研讨会登记表。
请帮忙

编码:

OleDbCommand command = new OleDbCommand("INSERT INTO Registration(Name,Branch,College,Contact)VALUES ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "')", conn);
if (radioButton1.Checked == true)
{
    command = new OleDbCommand("UPDATE Registration SET Year='" + radioButton1.Text + "' WHERE  contact='" + textBox4.Text + "'", conn);
}
else if (radioButton2.Checked == true)
{
    command = new OleDbCommand("UPDATE Registration SET Year='" + radioButton2.Text + "' WHERE  contact='" + textBox4.Text + "'", conn);
}
else if (radioButton3.Checked == true)
{
    command = new OleDbCommand("UPDATE Registration SET Year='" + radioButton3.Text + "' WHERE  contact='" + textBox4.Text + "'", conn);
}

if (radioButton4.Checked == true)
{
    command = new OleDbCommand("UPDATE Registration SET No_of_Candidates='" + radioButton4.Text + "' WHERE  contact='" + textBox4.Text + "'", conn);
}
else if (radioButton5.Checked == true)
{
    command = new OleDbCommand("UPDATE Registration SET No_of_Candidates='" + radioButton5.Text + "' WHERE  contact='" + textBox4.Text + "'", conn);
}

if (checkBox4.Checked == true)
{
    command = new OleDbCommand("UPDATE Registration SET Event='" + checkBox4.Text + "' WHERE  contact='" + textBox4.Text + "'", conn);
}
else if (checkBox5.Checked == true)
{
    command = new OleDbCommand("UPDATE Registration SET Event='" + checkBox5.Text + "' WHERE  contact='" + textBox4.Text + "'", conn);
}
else if (checkBox6.Checked == true)
{
    command = new OleDbCommand("UPDATE Registration SET Event='" + checkBox6.Text + "' WHERE  contact='" + textBox4.Text + "'", conn);
}
else if (checkBox7.Checked == true)
{
    command = new OleDbCommand("UPDATE Registration SET Event='" + checkBox7.Text + "' WHERE  contact='" + textBox4.Text + "'", conn);
}
else if (checkBox8.Checked == true)
{
    command = new OleDbCommand("UPDATE Registration SET Event='" + checkBox8.Text + "' WHERE  contact='" + textBox4.Text + "'", conn);
}
else if (checkBox9.Checked == true)
{
    command = new OleDbCommand("UPDATE Registration SET Event='" + checkBox9.Text + "' WHERE  contact='" + textBox4.Text + "'", conn);
}

myAdapptor.InsertCommand = command;
myAdapptor.SelectCommand = command;
command.Connection = conn;
conn.Open();
command.ExecuteNonQuery();
conn.Close();

使用下面的示例更新您的代码。用参数替换所有动态创建的SQL

using (var connection = new OleDbConnection("YouConnectionString"))
{
    connection.Open();
    using (var command = connection.CreateCommand())
    {
        // Not that all parameters are create with an @ at the front of the name
        command.CommandText = @"INSERT INTO Registration(Name, Branch, College, Contact)
                                VALUES (@THE_NAME, @THE_BRANCH, @THE_COLLEGE, @THE_CONTACT);";

        // Insert your parameters, change the OleDbType to what matches your field
        command.Parameters.Add("@THE_NAME", OleDbType.VarChar).Value = textBox1.Text;
        command.Parameters.Add("@THE_BRANCH", OleDbType.VarChar).Value = textBox2.Text;
        command.Parameters.Add("@THE_COLLEGE", OleDbType.VarChar).Value = textBox3.Text;
        command.Parameters.Add("@THE_CONTACT", OleDbType.VarChar).Value = textBox4.Text;
        var i = command.ExecuteNonQuery();

        if(i <= 0)
        {
            throw new Exception("Unable to create record.");
        }
    }
}
使用(var connection=new-OleDbConnection(“YouConnectionString”))
{
connection.Open();
使用(var command=connection.CreateCommand())
{
//并非所有参数都是在名称的前面使用@创建的
command.CommandText=@“插入注册(姓名、分支机构、学院、联系人)
值(@U名称、@U分支、@U学院、@U联系人);”;
//插入参数,将OLEDB类型更改为与字段匹配的类型
Add(“@THE_NAME”,OleDbType.VarChar).Value=textBox1.Text;
Add(“@THE_BRANCH”,OleDbType.VarChar).Value=textBox2.Text;
Add(“@THE_COLLEGE”,OleDbType.VarChar).Value=textBox3.Text;
command.Parameters.Add(“@THE_CONTACT”,OleDbType.VarChar).Value=textBox4.Text;
var i=command.ExecuteNonQuery();

如果(iUPDATE语句可以减少到这一行。
您总是更新相同的
联系人
,但根据所选复选框或单选按钮的值更改
年份、无候选项和事件的值。
因此,在返回要插入数据库的值的适当函数中隔离此逻辑。
请注意,年份是Access的保留关键字,因此需要在其周围加上方括号

string sqlCommand = @"UPDATE Registration SET [Year]=?, No_of_Candidates=?, Event=?
                     WHERE  contact=?";
command = new OleDbCommand(sqlCommand, conn);
command.Parameters.Add("@year", OleDbType.VarChar).Value = GetYearValue();  //integer??
command.Parameters.Add("@noc", OleDbType.VarChar).Value = GetNumberCanditatesValue();  //integer?
command.Parameters.Add("@evt", OleDbType.VarChar).Value = GetEventValue();  
command.Parameters.Add("@cont", OleDbType.VarChar).Value = TextBox4.Text;
现在您只需了解如何编写上面的GetXXXXXValue函数。
最后请注意,
年份
无候选项
似乎是数字字段而不是文本字段。如果是这种情况,则函数应返回适当的类型(Int32)OLEDB类型应更改为OLEDB类型。整数

您粘贴了准确的代码吗?没有任何条件的打开/关闭大括号的含义是什么?我在网站上看到这些大括号用于分组,但在添加这些“{}”之前,我也有分组框没有任何条件。我也有过这种怀疑,但它也没有显示出错误。很多人在过去说过这句话,但我觉得我需要重复一遍。在SQL语句中使用参数,会使数据库面临SQL注入的巨大风险!!!!!我希望.NET会抛出异常,如果使用类似的东西!为您提供信息,我将int使用sqldb conn我使用oledb连接使用access。如果有任何可能的解决方案,请回答。同样的原则适用,参数也可以用于OleDbk单选按钮和复选框如何?实际上,我只觉得它们有问题..plz帮助如何?您正在使用文本值更新事件,并且没有候选的您的数据库中的字段。上面的示例应该可以帮助您完成同样的操作。还请注意,我没有使用OleDbAdapter!不,我使用复选框和无线BTN作为更新字段。如果我将函数作为字符串作为返回类型编写,则说明并非代码的所有部分都会在编码过程中作为错误返回值。如果这是一个新问题,请发布一个新问题,然后单击ac如果有人帮助过你,请接受这里的一个答案。