数据插入SQL数据库的问题。 [英] Data Insert Problem into SQL Database.
问题描述
我的Windows窗体上有一些文本框。我想将这些文本框值插入数据库表,并在单击按钮时将更新的表显示到gridview中。这是按钮点击事件背后的插入代码。
// 打开连接
string connectionstring = ConfigurationManager.ConnectionStrings [ < span class =code-string> myDBconn]。ToString();
SqlConnection objconnection = new SqlConnection(connectionstring);
objconnection.Open();
// 插入火灾数据库的命令
string strInsertCommand = INSERT INTO party(party_name,party_mobile,updated_at)VALUES(' + txtAddPartyName.Text + ',' + txtAddPartyMobile.Text + ',' + DateTime.Now.ToShortDateString()+ ');
SqlCommand objCommand = new SqlCommand(strInsertCommand,objconnection);
objCommand.ExecuteNonQuery();
// close connection
objconnection.Close();
这是我的gridview更新代码。
objconnection.Open();
// 触发命令对象
objCommand = new SqlCommand( Select * from party,objconnection);
DataSet objDataset = new DataSet();
SqlDataAdapter objAdapter = new SqlDataAdapter(objCommand);
objAdapter.Fill(objDataset);
// 绑定来自UI的数据
dataGridView1.DataSource = objDataset.Tables [ 0 ];
// 关闭连接
objconnection.Close();
问题是当我填写所有文本框并点击插入按钮然后一切正常并且datagridview也显示新输入的条目但数据库表没有使用新条目更新。单击按钮时没有给出错误消息。
在此先感谢您,非常感谢您的帮助。
Sazzad
检查返回的值在ExecuteNonQuery中,可能是insert语句中存在一些问题,并使用try catch来跟踪异常
您的数据库可能会在Visual Studio完成的每次运行中被替换。右键单击visual studio解决方案资源管理器中的数据库文件,然后转到属性。在那里你需要将构建动作设置为内容并将复制到输出目录设置为复制如果更新选项。
I have some textbox on my windows form. I want to insert those textbox value into a database table and show the updated table into a gridview when a button clicked. Here is my Insert code behind the button click event.
// open connection
string connectionstring = ConfigurationManager.ConnectionStrings["myDBconn"].ToString();
SqlConnection objconnection = new SqlConnection(connectionstring);
objconnection.Open();
// command for insert fire database
string strInsertCommand= "INSERT INTO party ( party_name, party_mobile, updated_at) VALUES( '" + txtAddPartyName.Text + "', '" + txtAddPartyMobile.Text + "', '" + DateTime.Now.ToShortDateString() + "')";
SqlCommand objCommand = new SqlCommand(strInsertCommand, objconnection);
objCommand.ExecuteNonQuery();
// close connection
objconnection.Close();
And here is my gridview update code.
objconnection.Open();
//fire the command object
objCommand = new SqlCommand("Select * from party", objconnection);
DataSet objDataset = new DataSet();
SqlDataAdapter objAdapter = new SqlDataAdapter(objCommand);
objAdapter.Fill(objDataset);
//bind the data from UI
dataGridView1.DataSource = objDataset.Tables[0];
//close the connection
objconnection.Close();
Problem is when I fill all textbox and hit the insert button then everything is work fine and datagridview also shows newly entered entry but database table does not updated with the new entry. There is no error message given at button click.
Thanks in advance, your help is greatly appreciated.
Sazzad
Check the value that is returned in ExecuteNonQuery, it might be that there are some issues in insert statement and use try catch to track the exception
your database may be replace in every run you done by visual studio. right click on your database file in visual studio solution explorer and go to properties. there you need to set build action as content and copy to output directory as "copy if newer" option.
这篇关于数据插入SQL数据库的问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!