本文介绍了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!

推荐答案


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已经存在!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 09:33