Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.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# 在C中使用主键向数据库插入行的最佳方法是什么#_C#_Mysql_Sql - Fatal编程技术网

C# 在C中使用主键向数据库插入行的最佳方法是什么#

C# 在C中使用主键向数据库插入行的最佳方法是什么#,c#,mysql,sql,C#,Mysql,Sql,对不起,我的标题含糊不清。基本上,我有一个呼叫记录应用程序,它使用一个包含两个表(客户和记录)的数据库来添加、删除和搜索记录。我在记录中有一个名为“CallID”的列,用来帮助保持通话的唯一性,因此我可以使用CallID删除特定记录。但是,问题在于我添加了调用函数。我目前拥有它,因此CallID是列表中的项目数增加1: private void addcall_btn_Click(object sender, EventArgs e) { try {

对不起,我的标题含糊不清。基本上,我有一个呼叫记录应用程序,它使用一个包含两个表(客户和记录)的数据库来添加、删除和搜索记录。我在记录中有一个名为“CallID”的列,用来帮助保持通话的唯一性,因此我可以使用CallID删除特定记录。但是,问题在于我添加了调用函数。我目前拥有它,因此CallID是列表中的项目数增加1:

private void addcall_btn_Click(object sender, EventArgs e)
    {
        try
        {

            //This is my addcall click event that inserts a row in 'Records' table using what the user has entered into the fields
            string sql = "INSERT Records (CustomerID, CallID, CustomerName, Telephone, DateAndTime, Status, Description) VALUES (@CustomerID, @CallID, @CustomerName, @Telephone, @DateAndTime, @Status, @Description)";
            SqlConnection conn = ConnectionString();
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Connection = conn;
            cmd.Parameters.AddWithValue("CustomerID", customerID_cb.Text);
            cmd.Parameters.AddWithValue("CallID", (CallListBox.Items.Count + 1));
            cmd.Parameters.AddWithValue("DateAndTime", DateTime.Now);
            cmd.Parameters.AddWithValue("Status", status_cb.Text);
            cmd.Parameters.AddWithValue("Description", description_txt.Text);
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();

            MessageBox.Show("Record Created");
            this.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }
现在您可能已经知道,简单地将列表中的项目数增加1是一种笨拙的操作方式,并且在删除记录并希望添加更多记录后会导致问题。我想知道是否有一种方法可以做我想做的事情,但要用更好的方式:)


谢谢

CallID
设置为自动递增

ALTER TABLE Records AUTO_INCREMENT=1
然后更改insert语句,使其排除该字段

INSERT Records (CustomerID, CustomerName, Telephone, DateAndTime, Status, Description) VALUES (@CustomerID, @CustomerName, @Telephone, @DateAndTime, @Status, @Description)
CallID
字段的值随后将由数据库为任何后续添加的行处理。

以这种方式继续(如果您没有在数据库中使用自动增量),它可能会对您有所帮助

将最后一个值绑定到标签或文本框中

SqlConnection con = new SqlConnection("Your Connection String");
con.Open();
SqlCommand cmd = new SqlCommand("Select Max(CallID) from Yourtablename",con);    
SQlDataReader dr = cmd.ExecuteReader();
while(dr.Read())
{
    Label1.Text = dr.GetInt32(0).ToString();
}
con.Close();
在上面的代码中进行如下更改

cmd.Parameters.AddWithValue("CallID", Convert.ToInt32(Label1.Text));
将CallID列的数据类型设置为int


请让我知道进一步的问题。

您的代码中的CallListBox是什么?。哪些项目绑定在CallListBox中?您不能将CallID设置为数据库中的自动增量字段吗?CallListBox是我的windows窗体上的列表框的名称。列表框中的项目是记录表中的现有行,我将在编辑我的问题时澄清一些事情。继续。但我明白你的问题所在。您可以尝试在sql right中使用Max(),而不是将整个内容绑定到ListBox?嗯,自动递增听起来很理想。不幸的是,我是sql server新手。如何将列设置为自动递增?感谢您的帮助,简单地自动递增列似乎可以大大简化工作,但我非常感谢您的帮助。如果我有足够的代表+1你的答案,我会:D