C#连接sqlserver请问那写的不合理? public string strcon = "Integrated Security=SSPI;Initial Catalog='db123';Data Source='Ghost-1383119';User ID='sa';Password='sa';Connect Timeout=0"; try { SqlConnection conn = new SqlConnection(strcon); conn.Open(); strSQL = "select id,title,keyword from web where id<120 and id>98 order by id asc"; SqlDataAdapter da = new SqlDataAdapter(strSQL, conn); da.SelectCommand.CommandTimeout = 60000;
da.Fill(ds, "web"); conn.Close(); n = ds.Tables["web"].Rows.Count;
foreach (DataRow dr in ds.Tables["web"].Rows) { titleid = dr["id"].ToString(); if (titleid != null) { titleid = titleid.ToString(); }
long id = long.Parse(inputid); string strNextSQL = "select top 1 id,title,time from web where keyword='" + keyword + "' and id>" + id + " order by id asc";
SqlConnection con = new SqlConnection(strcon); con.Open(); SqlDataAdapter datar = new SqlDataAdapter(strPrevSQL, con);
SqlConnection mycon = new SqlConnection(strcon); mycon.Open();
long id = long.Parse(inputid); string strSQL = "select * from web where id=" + id; SqlDataReader dr = null; SqlCommand mycommand = new SqlCommand(strSQL, mycon); dr = mycommand.ExecuteReader(); while (dr.Read()) { titleid = dr["id"].ToString(); title = dr["title"].ToString(); } dr.Close(); mycommand.CommandText = "update web set show_path=1 where id=" + long.Parse(inputid); mycommand.ExecuteNonQuery(); mycon.Close();
------解决方案--------------------
1、public string strcon = "Integrated Security=SSPI;Initial Catalog='db123';Data Source='Ghost-1383119';User ID='sa';Password='sa';Connect Timeout=0"; 连接字符串,改成这样 public string strcon = @"Integrated Security=SSPI;Initial Catalog=db123;Data Source=Ghost-1383119;User ID=sa;Password=sa;Connect Timeout=0"; 2、sql语句改成 strSQL = "select [id],title,keyword from web where (id<120 and id>98) order by id";//默认asc 后面的sql语句也一样 3、 string strSQL = "select * from web where id=" + id;