C#中连接到数据库后怎么执行Sql语句?
2个回答
展开全部
C#中执行SQL语句
//SQL查询语句
public DataTable query(string sql)
{
//server=127.0.0.1;
DataTable dt=new DataTable();
//DataRow dr;
string connString = "server=127.0.0.1;uid=sa;pwd=;database=TJPTTC";
SqlConnection conn = new SqlConnection(connString);
SqlDataAdapter adapter = new SqlDataAdapter(sql,conn);
adapter.Fill(dt);
return dt;
}
//SQL非查询语句
public void noQuery(string sql)
{
//server=127.0.0.1;
string connString = "server=GLASSESONION;uid=sa;pwd=;database=TJPTTC";
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(sql,conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
//SQL查询语句
public DataTable query(string sql)
{
//server=127.0.0.1;
DataTable dt=new DataTable();
//DataRow dr;
string connString = "server=127.0.0.1;uid=sa;pwd=;database=TJPTTC";
SqlConnection conn = new SqlConnection(connString);
SqlDataAdapter adapter = new SqlDataAdapter(sql,conn);
adapter.Fill(dt);
return dt;
}
//SQL非查询语句
public void noQuery(string sql)
{
//server=127.0.0.1;
string connString = "server=GLASSESONION;uid=sa;pwd=;database=TJPTTC";
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(sql,conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询