1

我正在使用向导创建一个断开连接的数据集项目,但是,它允许我从表中更新和删除数据,但它不允许我插入新数据行并且仍然给我消息框“SUCCESS”!!!!

namespace Disconnected_Database_by_Wizard
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        SqlConnection myConnection = new SqlConnection();

        SqlDataAdapter myAdapter = null;

        private void Form1_Load(object sender, EventArgs e)
        {
            myConnection.ConnectionString = "Server=CompileMe;Database=MyDataBase;Integrated Security = True";
            myAdapter = new SqlDataAdapter("Select * from MyList", myConnection);
            myConnection.Open();
            myAdapter.Fill(dataSet1,"MyList");
            SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(myAdapter);

            myAdapter.InsertCommand = myCommandBuilder.GetInsertCommand();
            myAdapter.UpdateCommand = myCommandBuilder.GetUpdateCommand();
            myAdapter.InsertCommand = myCommandBuilder.GetDeleteCommand();
        }

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

                myAdapter.Update(dataSet1 ,"Mylist");
                MessageBox.Show("Success");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                myConnection.Close();

            }

        }
    }
}

提前致谢 :)

4

1 回答 1

0

我找到了问题,第三个命令行应该是:DeleteCommand 而不是 InsertCommand。

   { myAdapter.InsertCommand = myCommandBuilder.GetInsertCommand();
     myAdapter.UpdateCommand = myCommandBuilder.GetUpdateCommand();
      myAdapter.DeleteCommand = myCommandBuilder.GetDeleteCommand();
    }

代替 :

  { myAdapter.InsertCommand = myCommandBuilder.GetInsertCommand();
    myAdapter.UpdateCommand = myCommandBuilder.GetUpdateCommand();
    myAdapter.InsertCommand = myCommandBuilder.GetDeleteCommand();
  }
于 2013-04-11T20:05:06.317 回答