.net操作sqlserver数据库

 

 

appconfig配置:

<appSettings>
<add key="dbConnect" value="server=127.0.0.1;user id=sa;password=123456;database=benstar"/>
</appSettings>

保存:

GeneralSettingsConfig cfgsetting = GeneralSettingsConfig.Instance;
string connectingString = cfgsetting.GetAppConfig("dbConnect");//数据库连接,在APPCONFIG中配置的

SqlConnection sqlCon = new SqlConnection(connectingString);
sqlCon.Open();
SqlCommand sqlcmd = new SqlCommand();
sqlcmd.Connection = sqlCon;
StringBuilder sqlBuild = new StringBuilder();
sqlBuild.Append(string.Format("内部写sql语句"));
sqlcmd.CommandText = sqlBuild.ToString();
sqlcmd.ExecuteNonQuery();
sqlCon.Close();

查询:

GeneralSettingsConfig cfgsetting = GeneralSettingsConfig.Instance;
string connectingString = cfgsetting.GetAppConfig("dbConnect");

SqlConnection sqlCon = new SqlConnection(connectingString);
sqlCon.Open();
SqlCommand sqlcmd = new SqlCommand();
sqlcmd.Connection = sqlCon;

sqlcmd.CommandText = string.Format("select * from hebeijtb where card_no ='{0}'", tbCardNo.Text);

SqlDataAdapter sqlada = new SqlDataAdapter(sqlcmd);

DataSet ds = new DataSet();
sqlada.Fill(ds);

//这里处理ds数据

sqlCon.Close();

posted @ 2017-05-25 16:38  畅森  阅读(359)  评论(0编辑  收藏  举报