sql server 2005 connection string to .net application
-
2011년 11월 28일 월요일 오전 9:12
hi
i am using sqlserver 2005 express edition and visual studio 2010 .i am not able to connect sql server database to the .net application.Can anyone plz tell me how to do it.i wrote the below code.but its not working.the problem is with establishing the connection.pls let me know the correct connection string..
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con=new SqlConnection("server=JKC-PC/SQLEXPRESS;database=exam;Trusted_Connection=True;");
if (con.State == ConnectionState.Closed)
{
con.Open();
}
SqlCommand cmd = new SqlCommand("select admin_id from scheduleforexam_app_devp_tb");
Label2.Text = cmd.ExecuteScalar().ToString();
}
- 편집됨 sql learner12 2011년 11월 28일 월요일 오전 9:20
- 이동됨 Bob BeaucheminMVP 2011년 11월 28일 월요일 오후 4:51 Moved to the appropriate forum for best response (From:.NET Framework inside SQL Server)
- 유형 변경됨 sql learner12 2012년 2월 9일 목요일 오전 4:09
모든 응답
-
2011년 11월 28일 월요일 오전 9:22
boss try sending the connection obj (con) to sqlcommandobj as
SqlCommand cmd = new SqlCommand("select admin_id from scheduleforexam_app_devp_tb",con);
if u get any exception do post it- 편집됨 Prahalnathan 2011년 11월 28일 월요일 오전 9:22
-
2011년 11월 28일 월요일 오전 9:32
hi
thank you.. but i cud see its not getting connected to sql server
SqlConnection con = new SqlConnection("server=JAIPALMUSKU-PC/SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=schedularforexam");
if (con.State == ConnectionState.Closed)
{
con.Open();
}
SqlCommand cmd = new SqlCommand("select admin_id from scheduleforexam_app_devp_tb",con);
Label2.Text = cmd.ExecuteScalar().ToString();
error:
Line 26: if (con.State == ConnectionState.Closed) Line 27: { Line 28: con.Open(); Line 29: } Line 30: SqlCommand cmd = new SqlCommand("select admin_id from scheduleforexam_app_devp_tb",con);
- 편집됨 sql learner12 2011년 11월 28일 월요일 오전 9:33
-
2011년 11월 28일 월요일 오전 9:35
in the connction name DB name is missing..
it should be something like this
SqlConnection connectionString = new SqlConnection();
connectionString.ConnectionString = "Data Source =server; Database=DB1; Integrated Security=SSPI;Pooling=no";
post about how it goes and also the exception message- 편집됨 Prahalnathan 2011년 11월 28일 월요일 오전 9:36
-
2011년 11월 28일 월요일 오전 9:45
same error again..unable to open connection
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection connectionString = new SqlConnection();
connectionString.ConnectionString = "Data Source =JAIPALMUSKU-PC/SQLEXPRESS; Database=schedularforexam; Integrated Security=SSPI;Pooling=no";
SqlCommand cmd = new SqlCommand("select admin_id from scheduleforexam_app_devp_tb",connectionString);
if (connectionString.State == ConnectionState.Closed)
{
connectionString.Open();
}
Label2.Text = cmd.ExecuteScalar().ToString();
}
}
- 편집됨 sql learner12 2011년 11월 28일 월요일 오전 9:51
-
2011년 11월 28일 월요일 오전 9:53is the server name and DB correct?.
- 편집됨 Prahalnathan 2011년 11월 28일 월요일 오전 9:54
-
2011년 11월 28일 월요일 오전 9:58yes i am able to connect to the database and retrieve the data by using the smart tag above the dropbox..i cud retrieve all the feilds from the database to the form..but not able connect it by writing the code.
- 편집됨 sql learner12 2011년 11월 28일 월요일 오전 10:01
-
2011년 11월 28일 월요일 오후 12:13
- 답변으로 표시됨 Allen Li - AI3Microsoft Contingent Staff, Moderator 2011년 12월 4일 일요일 오전 3:30
-
2011년 11월 30일 수요일 오전 2:03중재자
Hi sql learner12,
The connection can’t be opened caused by the incorrect Connection String. You can use Server Explorer to get the correct Connection String. Just do like this:
1. Find out the database which you want to connect in the Server Explorer;
2. Double click the database, then you will find the red cross attached to the database icon has disappeared, it means you have already connected to the database;
3. Look at Properties Window, there’s a property named Connection String, please copy the value of the property and paste it to your code.
Best Regards,
Allen Li [MSFT]
MSDN Community Support | Feedback to us
- 답변으로 표시됨 Allen Li - AI3Microsoft Contingent Staff, Moderator 2011년 12월 4일 일요일 오전 3:31
-
2012년 2월 9일 목요일 오전 3:37thank you Allen..
akhila

