窗体数据库连接(INSERT) [英] window form database connection(INSERT)
本文介绍了窗体数据库连接(INSERT)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好
我在数据库中插入一个字符串和一个int。没有错误,但下面的代码不起作用。我认为这是在我的WPF应用程序上工作的连接字符串。你能告诉我出了什么问题吗? TIA! :D
Hi guys
i am inserting a string and a int into a database. There is no error but the code below didn''t work. i am thinking it is the connectionstring which work on my WPF application. Can you please advise me on what went wrong. TIA! :D
string a = textBox1.Text;
string b = comboBox1.Text;
string c = a + b;
int l = 0;
if (textBox1.Text == "")
{
MessageBox.Show("Please Enter a Video's Name!", "Information",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else if (comboBox1.Text == "")
{
MessageBox.Show("Please select Video's extension", "Information",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Video.MDF;Integrated Security=True;User Instance =True");
SqlCommand comm = new SqlCommand("INSERT INTO Video VALUES('" + a + b + "'," + l + ")", con);
SqlDataAdapter da = new SqlDataAdapter(comm);
con.Open();
comm.ExecuteNonQuery();
}
推荐答案
没什么看起来可疑的在你的代码中除了ConnectionString ...
AttchDbFileName做什么??
无论如何你可能想尝试正常的ConnectionString下面:
Hi,
Nothing looks suspicious in your code except for the ConnectionString...
What does AttchDbFileName do??
Anyways you might want to try a normal ConnectionString as below:
connectionString="Data Source=MY-PC;Initial Catalog=Sample; Integrated Security=true
如果我们知道它工作..
干杯
Let us know if it works..
Cheers
用断点调试你的代码。我希望这能帮助你纠正连接字符串当你真正得到错误。其他明智的它很好。
也试试这个
Debug your code with a break point. I hope that will help you in correcting connection string when you actually got the error. Other wise its is fine.
also try this
SqlCommand comm = new SqlCommand("INSERT INTO Video VALUES(''" + (a + b) + "''," + l + ")", con);
而不是
instead of
SqlCommand comm = new SqlCommand("INSERT INTO Video VALUES(''" + a + b + "''," + l + ")", con);
我已经解决了,问题在数据库上。我已经将数据库更改为sql server 2008而不是visual studio中的数据库
感谢大家的帮助:D
I have solve it, the problem lies on the database. i have change the database to sql server 2008 instead of the one in visual studio
thanks everyone for your help :D
这篇关于窗体数据库连接(INSERT)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文