代码中未显示任何错误,但未将行添加到表中

时间:2014-06-17 07:57:53

标签: c# sql

public partial class add_dept : Form
{
    public add_dept()
    {
        InitializeComponent();
    }
    DataSet1 ds = new DataSet1();
    DataSet1TableAdapters.deptTableAdapter dta = new DataSet1TableAdapters.deptTableAdapter();

    private void button1_Click(object sender, EventArgs e)
    {
        if (textBox1.Text != "" && textBox2.Text != "" && textBox3.Text != "" && textBox4.Text != "")
        {
            dta.Fill(ds.dept);
            bool flag = false;
            for (int i = 0; i <= ds.dept.Rows.Count - 1; i++)
            {
                if(textBox1.Text==ds.dept.Rows[i]["dept_name"].ToString())
                {
                    flag=true;
                    break;
                }
            }
            if (flag==false)
            {
                dta.Fill(ds.dept);
                DataRow d=ds.dept.NewRow();
                d["dept_name"]=textBox1.Text;
                d["emp_no"]=textBox2.Text;
                d["location"]=textBox3.Text;
                d["project_no"]=textBox4.Text;
                ds.dept.Rows.Add(d);
                dta.Update(ds.dept);
                MessageBox.Show("ROW ADDED");
            }
            else
            {
                MessageBox.Show("Department already exists");
            }
        }
    }

表定义是

CREATE TABLE [dbo].[dept] 
(
    [dept_id]    INT          IDENTITY (100, 1) NOT NULL,
    [dept_name]  VARCHAR (25) NULL,
    [emp_no]     INT          NULL,
    [location]   VARCHAR (50) NULL,
    [project_no] INT          NULL,
    PRIMARY KEY CLUSTERED ([dept_id] ASC)
);

1 个答案:

答案 0 :(得分:0)

您是否尝试过使用

d["emp_no"]=int.Parse(textBox2.Text);

而不是

d["emp_no"]=textBox2.Text;

表示整数列?