我需要將文本框值添加到SQL Server數據庫表。下面是我的代碼:使用c將文本框值插入到sql服務器#
private void button1_Click(object sender, EventArgs e)
{
string str = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\timetablesystem.mdf;Integrated Security=True;User Instance=True";
SqlConnection con = new SqlConnection(str);
string qry = "insert into SubjectMaster (SubjectName) values (@TxtSubjectName)";
con.Open();
SqlCommand cmd = new SqlCommand(qry, con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@TxtSubjectName", TxtSubjectName.Text);
cmd.ExecuteNonQuery();
MessageBox.Show("Added Successfully!!");
con.Close();
}
不過,數據應在表中未添加......請幫我... 感謝烏拉圭回合的幫助...
你有文件'timetablesystem.mdf'您的項目文件和上市之間其屬性「複製到輸出目錄」設置爲「始終複製」? – Steve 2013-02-27 11:48:59
在將代碼放入代碼之前,您是否嘗試先調試您的查詢? – user15 2013-02-27 11:49:26
可能的重複:http://stackoverflow.com/questions/9363061/how-to-insert-record-into-a-sql-server-express-database-table – 2013-02-27 11:50:00