我只是C#上的一個begginer,所以我需要太多的幫助。現在的問題是,我設計了一個Windows窗體,其中有許多字段,如名字,姓氏,地址等。現在我想要做的是,當我填寫表單並單擊插入按鈕,所有的信息進入數據庫。有誰知道這是怎麼做到的嗎?如何使用C#語言在數據庫中插入記錄?
private void button1_Click(object sender, System.EventArgs e)
{
string connetionString = null;
SqlConnection cnn ;
SqlDataAdapter adapter = new SqlDataAdapter();
string sql = null;
connetionString = "Data Source=UMAIR;Initial Catalog=Air; Trusted_Connection=True;" ;
cnn = new SqlConnection(connetionString);
sql = "insert into Main (Firt Name, Last Name) values(textbox2.Text,textbox3.Text)";
try
{
cnn.Open();
adapter.InsertCommand = new SqlCommand(sql, cnn);
adapter.InsertCommand.ExecuteNonQuery();
MessageBox.Show ("Row inserted !! ");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
做任何事情現在就加入?因爲它看起來像「textbox2.text」和「textbox3.text」值可能是 – Keerigan
沒有什麼。我用這個來檢查,但都是徒勞的。 –
數據庫中是否存在一個名爲「Firt Name」和「Last Name」的列?他們之間真的有空間嗎? – Keerigan