Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/67.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
winforms C#使用sql server 2008_C#_Sql_Sql Server_Winforms_If Statement - Fatal编程技术网

winforms C#使用sql server 2008

winforms C#使用sql server 2008,c#,sql,sql-server,winforms,if-statement,C#,Sql,Sql Server,Winforms,If Statement,我正在使用这段代码,但是如果我的表中没有数据,它将无法接受,则会出现问题。 因此,我想使用,如果没有数据存在,则应将CustomerId设为1,因为没有行,所以它将为空,因此您可以: private void fillcode() { try { SqlConnection con = new SqlConnection("Data Source=ANISH;Initial Catalog=HM;Integrated Security=True");

我正在使用这段代码,但是如果我的表中没有数据,它将无法接受,则会出现问题。
因此,我想使用,如果没有数据存在,则应将CustomerId设为1,因为没有行,所以它将为空,因此您可以:

private void fillcode()
{
    try
    {
        SqlConnection con = new SqlConnection("Data Source=ANISH;Initial Catalog=HM;Integrated Security=True");
        con.Open();
        string s = "select max(CustomerId) as Id from CustomerDetails";
        SqlCommand cmd = new SqlCommand(s, con);
        SqlDataReader dr = cmd.ExecuteReader();
        dr.Read();
        int i = Convert.ToInt16(dr["Id"].ToString());
        sid.Text = (i + 1).ToString();
        con.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
您还应该查看专为单个结果设计的
ExecuteScalar

尝试这样做

"select isnull(max(CustomerId), 1) as Id from CustomerDetails"

测试系统中是否有任何ID。如果没有,则使用1。顺便说一句,您应该编辑问题的标题,使其更能描述您的问题,而不是一堆标签。看起来您正试图根据表中该列的最后一个最高值创建递增的客户id。我强烈建议通过将列设置为您的
标识
,让SQL Server为您做到这一点。它会以一种更安全的方式为你做这件事。很好!但是如果他以后需要写入ID列,这将变得很棘手,因为执行
isnull
将使列成为只读。是的,这是一个很好的观点,我假设这是一个只读查找。考虑到列的可能功能,我建议将1改为0可能是一个安全的假设,但你永远不知道
ExecuteScalar
正是OP需要清理的地方。如果我使用上面的代码,它不起作用,它会显示“输入字符串格式不正确”,那么我现在可以做什么
private void fillcode()
{
    try
    {
        SqlConnection con = new SqlConnection("Data Source=ANISH;Initial Catalog=HM;Integrated Security=True");
        con.Open();
        string s = "select max(CustomerId) as Id from CustomerDetails";
        SqlCommand cmd = new SqlCommand(s, con);
        SqlDataReader dr = cmd.ExecuteReader();
        if(dr.Read())
        {
            int i = Convert.ToInt16(dr["Id"].ToString());
            sid.Text = (i + 1).ToString();
        }
        else
        {
            sid.Text = "1"
        } 
        con.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}