常用语句 备用
命名空间:
using System.Data.SqlClient;
连接打开数据库
SqlConnection conn = new SqlConnection("Data Source=计算机名;Initial Catalog=数据库名;User ID=帐号;Password=密码;");
conn.Open();
用完之后记得
conn.close();
查询
SqlDataAdapter adapter = new SqlDataAdapter();
SqlCommand cmdSelect = new SqlCommand("select * from 表名 where 字段名=" + 值);
cmdSelect.Connection = conn;
adapter.SelectCommand = cmdSelect;
DataSet ds = new DataSet();
adapter.Fill(ds);
foreach (DataRow row in ds.Tables[0].Rows)
{
string a=row[0].tostring();
string b=row["id"].tostring();
}
更新删除等:
SqlCommand cmd = new SqlCommand(sqlStr, conn);
//MessageBox.Show(sqlStr);
cmd.ExecuteScalar();
查询数量:
string sqlStr = "select count(*) from 表名 where 字段名='" +值 + "'";
SqlCommand cmd = new SqlCommand(sqlStr, conn);
//MessageBox.Show(sqlStr);
string count=cmd.ExecuteScalar().ToString();