SQL错误:ID已经存在! [英] SQL Error: ID already exists!

查看:104
本文介绍了SQL错误:ID已经存在!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!
在我的以下C#代码中,记录未添加到数据库中.错误消息表明扇区ID已经存在.

Hi everybody!
In my following C# code the records are not getting added to the database. The error messages says that the sector id already exists.

private void btnAdd_Click(object sender, EventArgs e)
{
    SqlDataAdapter adp = new SqlDataAdapter("Select * from Sector", con);
    DataSet ds = new DataSet();
    adp.Fill(ds, "Sector");
    if (txtSectorID.Text == "")
    {
        MessageBox.Show("Please Enter Sector ID");
        txtSectorID.Focus();
        return;
    }
    if (txtDesc.Text == "")
    {
        MessageBox.Show("Please Enter some Description");
        txtDesc.Focus();
        return;
    }
    else if (txtFCFares.Text == "")
    {
        MessageBox.Show("Please Enter First Class Fares");
        txtFCFares.Focus();
        return;
    }
    if (!(txtFCFares.Text == ""))
    {
        string fstr;
        string fstr1 = null;
        bool fcheck = false;
        int fi = 0;
        int fasci = 0;
        fstr = txtFCFares.Text;
        for (fi = 1; fi <= fstr.Length; fi++)
        {
            fstr1 = fstr.Substring(fi - 1, 1);
            fasci = System.Convert.ToInt32(fstr1[0]);
            if (fasci >= 48 & fasci <= 57)
            {
                fcheck = true;
            }
            else
            {
                fcheck = false;
                break;
            }
        }
        if (fcheck == false)
        {
            MessageBox.Show("Enter valid Fares");
            txtFCFares.Text = "";
            txtFCFares.Focus();
            return;
        }
    }
    if (txtBCFares.Text == "")
    {
        MessageBox.Show("Please Enter Business Class Fares");
        txtBCFares.Focus();
        return;
    }
    if (!(txtBCFares.Text == ""))
    {
        string bstr;
        string bstr1 = null;
        bool bcheck = false;
        int bi = 0;
        int basci = 0;
        bstr = txtBCFares.Text;
        for (bi = 1; bi <= bstr.Length; bi++)
        {
            bstr1 = bstr.Substring(bi - 1, 1);
            basci = System.Convert.ToInt32(bstr1[0]);
            if (basci >= 48 & basci <= 57)
            {
                bcheck = true;
            }
            else
            {
                bcheck = false;
                break;
            }
        }
        if (bcheck == false)
        {
            MessageBox.Show("Enter Valid Fares");
            txtBCFares.Focus();
            return;
        }
    }
    if (txtECFares.Text == "")
    {
        MessageBox.Show("Please Enter Economy Class Fares");
        txtECFares.Focus();
        return;
    }
    if (!(txtECFares.Text == ""))
    {
        string estr;
        string estr1 = null;
        bool echeck = false;
        int ei = 0;
        int easci = 0;
        estr = txtECFares.Text;
        for (ei = 1; ei <= estr.Length; ei++)
        {
            estr1 = estr.Substring(ei - 1, 1);
            easci = System.Convert.ToInt32(estr1[0]);
            if (easci >= 48 & easci <= 57)
            {
                echeck = true;
            }
            else
            {
                echeck = false;
                break;
            }
        }
        if (echeck == false)
        {
            MessageBox.Show("Enter Valid Fares");
            txtECFares.Focus();
            return;
        }
    }
    try
    {
        table = ds.Tables["Sector"];
        row = table.NewRow();
        row[0] = txtSectorID.Text;
        row[1] = txtDesc.Text;
        row[2] = txtFCFares.Text;
        row[3] = txtBCFares.Text;
        row[4] = txtECFares.Text;
        table.Rows.Add(row);
        SqlCommandBuilder comm = new SqlCommandBuilder(adp);
        adp.InsertCommand = comm.GetInsertCommand();
        adp.Update(ds, "Sector");
        adp.Fill(ds);
        MessageBox.Show("New Entry Saved");
        SqlDataAdapter adp2 = new SqlDataAdapter("Select * from Sector", con);
        DataSet ds2 = new DataSet();
        adp.Fill(ds2, "Sector");
        dgvSectorDetails.DataSource = ds2.Tables[0].DefaultView;
        txtSectorID.Text = "";
        txtDesc.Text = "";
        txtFCFares.Text = "";
        txtBCFares.Text = "";
        txtECFares.Text = "";
    }
    catch
    {
        MessageBox.Show("Sector ID already Exist");
    }
}



有人可以告诉我这里出了什么问题吗?

感谢您的考虑!



Can anybody please tell what is going wrong here?

Thanks for your consideration!

推荐答案

仅因为您的错误消息是"Sector ID已经存在",并不一定意味着这是真正的原因.在您的catch语句中添加一个异常变量,并报告(或记录)实际错误,并包含所有内部异常数据.遇到这种情况时,您可以开始查看可能的原因!
Just because your error message is "Sector ID already exists" does not nescessarily mean that this is the true reason. Add an exception variable to your catch statement, and report (or log) the actual error, complete with any inner exception data. When you have that, you can start to look at what may be causing it!


您在这里的代码有点野蛮.在您的try catch语句中,您明确地假设,如果发生异常,则一定是因为扇区ID已经存在.这不是完成的方式.如果预计会发生异常,请使用正确的catch块.如果您使用多个捕获块,请确保要捕获的异常是从最具体到最不具体的顺序排列的.

You''re being a bit brutal in your code here. In your try catch statement you plainly assume that if an exception occurs it must be because the Sector ID already exists. This is not how this is done. If an exception is expected use the correct catch block. If you use multiple catch blocks make sure the Exceptions that are being caught are ordered from most specific to least specific.

try
{
    table = ds.Tables["Sector"];
    row = table.NewRow();
    row[0] = txtSectorID.Text;
    row[1] = txtDesc.Text;
    row[2] = txtFCFares.Text;
    row[3] = txtBCFares.Text;
    row[4] = txtECFares.Text;
    table.Rows.Add(row);
    SqlCommandBuilder comm = new SqlCommandBuilder(adp);
    adp.InsertCommand = comm.GetInsertCommand();
    adp.Update(ds, "Sector");
    adp.Fill(ds);
    MessageBox.Show("New Entry Saved");
    SqlDataAdapter adp2 = new SqlDataAdapter("Select * from Sector", con);
    DataSet ds2 = new DataSet();
    adp.Fill(ds2, "Sector");
    dgvSectorDetails.DataSource = ds2.Tables[0].DefaultView;
    txtSectorID.Text = "";
    txtDesc.Text = "";
    txtFCFares.Text = "";
    txtBCFares.Text = "";
    txtECFares.Text = "";
}
catch(SqlException sqlEx)
{
    MessageBox.Show(sqlEx.Message);
}
//. Here could follow more catch blocks.
//. Each successor being less specific than the previous one.
//. Until finally the most unspecific namely Exception is handled.
catch(Exception ex)
{
    MessageBox.Show(ex.Message);
}



供您研究的一些东西: http://msdn.microsoft.com/zh-CN/library/system.data.sqlclient.sqlexception(v=VS.100).aspx [ http://msdn.microsoft.com/en-us/library/s7fekhdy(v = VS.80).aspx [ ^ ]

问候,

—MRB



Some stuff for you to research:http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlexception(v=VS.100).aspx[^] and also this information should help you: http://msdn.microsoft.com/en-us/library/s7fekhdy(v=VS.80).aspx[^]

Regards,

—MRB


这篇关于SQL错误:ID已经存在!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆