//HAN-PC\SQLEXPRESS 是我的服务器名称,sql Server的
//uid是han-PC\han 没密码
然后在app code
public static string connstring = ConfigurationManager.AppSettings["cn"];
SqlDataAdapter dap = new SqlDataAdapter(sql, connstring);
但是好像连不到数据库,怎么回事,求指教,本人菜鸟一只
给你一个dbhelper、 public static class dbhelper { private static sqlconnection connection = null; /// <summary> /// 得到数据库连接 /// </summary> public static sqlconnection connection { get { string connetionstring = configurationmanager.connectionstrings["myconnectionstring"].connectionstring;//这里的连接字符串写在配置文件里、 if (connection == null) { connection = new sqlconnection(connetionstring); connection.open(); } else if (connection.state == connectionstate.closed) { connection = new sqlconnection(connetionstring); connection.open(); } else if (connection.state == connectionstate.broken) { connection.close(); connection.open(); } return connection; } } /// <summary> /// 增、删、改、操作 /// </summary> /// <param name="sqlstr">sql语句</param> /// <param name="type">执行类型</param> /// <param name="para">参数数组</param> /// <returns>影响行数</returns> public static int executecommand(string sqlstr, commandtype type, sqlparameter[] para) { try { using (sqlcommand cmd = new sqlcommand(sqlstr, connection)) { cmd.commandtype = type; if (para != null && para.length > 0) cmd.parameters.addrange(para); return cmd.executenonquery(); } } catch (sqlexception ex) { throw ex; } } /// <summary> /// 查询单条记录 /// </summary> /// <param name="sqlstr">sql字符串</param> /// <param name="type">执行类型</param> /// <param name="para">参数数组</param> /// <returns>单个object值</returns> public static object getscalar(string sqlstr, commandtype type, sqlparameter[] para) { try { using (sqlcommand cmd = new sqlcommand(sqlstr, connection)) { cmd.commandtype = type; if (para != null && para.length > 0) cmd.parameters.addrange(para); return cmd.executescalar(); } } catch (sqlexception ex) { throw ex; } } /// <summary> /// 查询多条记录 /// </summary> /// <param name="sqlstr">sql字符串</param> /// <param name="type">执行类型</param> /// <param name="para">参数数组</param> /// <returns>sqldatareader对象</returns> public static sqldatareader getreader(string sqlstr, commandtype type, sqlparameter[] para) { try { using (sqlcommand cmd = new sqlcommand(sqlstr, connection)) { cmd.commandtype = type; if (para != null && para.length > 0) cmd.parameters.addrange(para); return cmd.executereader(); } } catch (sqlexception ex) { throw ex; } } /// <summary> /// 查询多条记录 /// </summary> /// <param name="sqlstr">sql字符串</param> /// <param name="type">执行类型</param> /// <param name="para">参数数组</param> /// <returns>dataset对象</returns> public static datatable getdataset(string sqlstr, commandtype type, sqlparameter[] para) { try { using (sqlcommand cmd = new sqlcommand(sqlstr, connection)) { cmd.commandtype = type; if (para != null && para.length > 0) cmd.parameters.addrange(para); dataset dataset = new dataset(); new sqldataadapter(cmd).fill(dataset); return dataset.tables[0]; } } catch (sqlexception ex) { throw ex; } } }
配置文件(web.config)
<add name="myconnectionstring" connectionstring="data source=.;initial catalog=dbname;integrated security=true" providername="system.data.sqlclient"/>
这里我的数据库用的是sql2005、密码为空、
祝你好运、、、