连接数据库时遇到的一个大问题 [英] A big problem in connecting to database
问题描述
嗨!
我的程序工作得非常好,直到昨天,当我运行它时,我的网格视图从sql数据库的数据表中获取数据源没有表现出什么。我试图理解断点有什么问题,我明白程序会跳过这部分并且不会连接到数据库:
Hi!
My program worked very well untill yesterday, when i was running it, my grid view that got data source from a data table of sql data base didn't show any thing. i tried to understand what was wrong with a break point, i understood that program jumps this part and doesn't connect to data base:
SqlConnection cs = new SqlConnection("Data Source=.\\local_2008; Initial Catalog=Library; User Id=userLibrary; Password=123456");
有趣的部分是,它没有显示任何错误,当我使用try / catch异常并没有显示任何关于错误的内容。
让我疯狂的是当我在这一行使用断点来获取错误并选择部件并使用Quick Watch显示没有错误,然后我点击F5继续该程序工作,并显示表!!!!!!
请帮我解决这个令人难以置信的问题
谢谢
the interesting part is that, it doesn't show any error and when i used try/catch the exception doesn't show anything about error.
and what made me crazy is that when i use a breakpoint on this line to get the error and select the part and use Quick Watch it shows no error and then i click f5 to continue the program works as it should and shows the table!!!!!!
please help me to solve this unbelievable problem
thank you
SqlConnection cs = new SqlConnection("Data Source=.\\local_2008; Initial Catalog=Library; User Id=userLibrary; Password=123456");
SqlCommand cmd = new SqlCommand("searchBook", cs);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@biCode", SqlDbType.Int).Value = biCode;
cmd.Parameters.Add("@biName", SqlDbType.NVarChar).Value = biName;
cmd.Parameters.Add("@biAuthor", SqlDbType.NVarChar).Value = biAuthor;
cmd.Parameters.Add("@biTranslator", SqlDbType.NVarChar).Value = biTranslator;
cmd.Parameters.Add("@biPublisher", SqlDbType.NVarChar).Value = biPublisher;
cmd.Parameters.Add("@biSubject", SqlDbType.Int).Value = biSubject;
cmd.Parameters.Add("@biStatus", SqlDbType.Int).Value = biStatus;
cmd.Parameters.Add("@biIssueDate", SqlDbType.VarChar).Value = biIssueDate;
cmd.Parameters.Add("@biLink", SqlDbType.NVarChar).Value = biLink;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
好消息:
我再次使用app.config并且旧的错误没有再次发生但现在它从da.Fill(dt)跳出来
实例失败
错误我现在该怎么办?
新部分:
Good news:
I used app.config again and the old error didn't happened again but now it jumps out from da.Fill(dt) with
Instance failure
error now what should i do?
new part:
string str = System.Configuration.ConfigurationManager.AppSettings["cs"];
SqlConnection cs = new SqlConnection(str);
推荐答案
对于dataadapter,您尚未传递连接字符串。尝试
for dataadapter you have not passed the connectionstring. try
DataAdapter da=New DataAdapter(query,ConString)
它应该可以工作。您无需手动打开数据适配器的连接。它自动完成。
it should work.and you need not manually open a connection for a dataadapter.it does that automatically.
尝试如下...
Try like below...
SqlConnection cs = new SqlConnection("Data Source=.;Initial Catalog=Library;User ID=userLibrary;Password=123456");
特别感谢谁的帮助
我不知道第一个问题是什么,但是当我使用app.config时它就解决了。
和第二部分
来自连接字符串。当你使用app.config时要小心,因为它是XML并且不会转到//并且你应该自己动手:D
BYE BYE
special thanks to who help
I don't know what was the first problem but when i used app.config it solved.
and about the second part
it was from the connect string. be careful when you use app.config because it's XML and doesn't turn // to / and you should do that your self :D
BYE BYE
这篇关于连接数据库时遇到的一个大问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!