取连接字符串
<remove name="LocalSqlServer" />
<add name="SqlConnStr" connectionString="user id=xx;password=xx;initial catalog=database_name;data source=.\sqlxxxx" />
</connectionStrings>
取连接字符串:
<add key="connstring" value="uid=xx;pwd=xx;database=batabase_name;server=(local)" />
</appsettings>
据说两者通用,但是第二种是asp.net2.0的新特性,建议使用第二种。
其实我一直有个疑问,两个字符串中的UID;PWD;和User ID; Password;是否等价。
根据网上我查到的资料是可以互换通用的。
网上找到了个貌似更详细的解释:
连接SQL Server数据库的机制与连接Access的机制没有什么太大的区别,只是改变了Connection对象和连接字符串中的不同参数.
首先,连接SQL Server使用的命名空间不是"System.Data.OleDb",而是"System.Data.SqlClient".
其次就是他的连接字符串了,我们一个一个参数来介绍(注意:参数间用分号分隔):
"user id=sa":连接数据库的验证用户名为sa.他还有一个别名"uid",所以这句我们还可以写成"uid=sa".
"password=":连接数据库的验证密码为空.他的别名为"pwd",所以我们可以写为"pwd=".
这里注意,你的SQL Server必须已经设置了需要用户名和密码来登录,否则不能用这样的方式来登录.如果你的SQL Server设置为Windows登录,那么在这里就不需要使用"user id"和"password"这样的方式来登录,而需要使用"Trusted_Connection=SSPI"来进行登录.
"initial catalog=Northwind":使用的数据源为"Northwind"这个数据库.他的别名为"Database",本句可以写成"Database=Northwind".
"Server=YourSQLServer":使用名为"YourSQLServer"的服务器.他的别名为"Data Source","Address","Addr".如果使用的是本地数据库且定义了实例名,则可以写为"Server=(local)\实例名";如果是远程服务器,则将"(local)"替换为远程服务器的名称或IP地址.
"Connect Timeout=30":连接超时时间为30秒.
在这里,建立连接对象用的构造函数为:SqlConnection.
——————————————————————————————
下面是ADO.NET对象操作数据库代码
public static class DBHELP { public static string connectionString = ConfigurationManager.ConnectionStrings["con"].ConnectionString; /// <summary> /// 修改 /// </summary> public static bool DataUpdate(Vote vote) { int res = -1; // string sqlCon = @"server =.;database=strike;Integrated Security=True"; SqlConnection conn = new SqlConnection(); conn.ConnectionString = connectionString; string sqlStr = "update vote set ipaddress='"+vote.IPaddress+"' where id="+vote.ID; SqlCommand cmd = new SqlCommand(sqlStr, conn); conn.Open(); res = cmd.ExecuteNonQuery(); conn.Close(); if (res > 0) { return true; } return false; } /// <summary> /// 添加 /// </summary> public static void AddRecord() { int res = -1; //string sqlCon = "server =.;database=strike;Integrated Security=True"; SqlConnection conn = new SqlConnection(); conn.ConnectionString = connectionString; string b_title = "标"; string b_content = "容"; string b_author = "作"; string addtime = "2011-11-11"; int b_cid = 102; string sqlStr2 = string.Format("insert book(b_cid,b_title,b_content,b_author,b_isdel,b_addtile,b_money) values({0},'{1}','{2}','{3}',0,'{4}',10)", b_cid, b_title, b_content, b_author, addtime); string sqlStr = "insert book(b_cid,b_title,b_content,b_author,b_isdel,b_addtile,b_money) values(103,'dsf','dfdf','dfd',0,'2011-11-11',10)"; SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = sqlStr2; conn.Open(); res = cmd.ExecuteNonQuery(); conn.Close(); if (res > 0) { Console.WriteLine("受影响" + res); } } /// <summary> /// 删除数据 /// </summary> public static void DelRecord() { int res = -1; //string sqlCon = "server =.;database=strike;Integrated Security=True"; SqlConnection conn = new SqlConnection(); conn.ConnectionString = connectionString; string sqlStr = "delete book where b_id=18"; SqlCommand cmd = new SqlCommand(sqlStr, conn); conn.Open(); res = cmd.ExecuteNonQuery(); conn.Close(); if (res > 0) { Console.WriteLine(res + "行受影响"); } } /// <summary> /// 查询多行数据---DataReader逐行读取,每次读一行 /// </summary> public static List<Vote> QueryList(int pagesize, int pageindex) { SqlConnection con = new SqlConnection(connectionString); string sqlStr = "select top " + pagesize+" ID,IP from vote where ID not in (select top " + pagesize * (pageindex - 1) + " id from vote)"; SqlCommand cmd = new SqlCommand(sqlStr, con); con.Open(); SqlDataReader dr = cmd.ExecuteReader(); List<Vote> votes = new List<Vote>(); if (dr.HasRows) { while (dr.Read())//dr每read()一次就读取一行 { Vote v = new Vote(); v.ID = Convert.ToInt32(dr["id"]); v.IP = dr["ip"].ToString(); votes.Add(v); } return votes; // Console.WriteLine("有数据"); } else { Console.WriteLine("无数据"); } return null; } /// <summary> /// 用ExecuteScalar()方法 ,返回单个值(Object)(查询结果第一行第一列的值) /// </summary> public static void QuerySingle() { SqlConnection conn = new SqlConnection(connectionString); string sqlStr = "select * from book"; SqlCommand cmd = new SqlCommand(sqlStr, conn); conn.Open(); //返回的是查询结果集第一单元格的值,返回的是object类型 object obj = cmd.ExecuteScalar(); conn.Close(); Console.WriteLine(obj.ToString()); } /// <summary> /// 调用存储过程 /// </summary> public static void QueryListByProc2() { string sqlCon = "server=.;database=strike;Integrated Security=True;"; SqlConnection conn = new SqlConnection(sqlCon); SqlCommand cmd = new SqlCommand("uspo_GetPageBooks", conn); cmd.CommandType = CommandType.StoredProcedure; SqlParameter[] paras ={ new SqlParameter("@pageIndex",SqlDbType.Int,4), new SqlParameter("@pageSize",SqlDbType.Int,4) }; paras[0].Value = 1; paras[1].Value = 3; cmd.Parameters.AddRange(paras); //有适配器读取数据 SqlDataAdapter da = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); da.Fill(dt); foreach (DataRow dr in dt.Rows) { Console.WriteLine(dr[0].ToString() + "__" + dr[1].ToString() + "__" + dr[2].ToString()); } } /// <summary> /// 使用适配器读取数据 /// </summary> public static void QueryListByAdapter() { SqlConnection conn = new SqlConnection(connectionString); string sqlStr = "select * from book"; //创建适配器对象,高数它要做些什么事情,走哪条路去 SqlDataAdapter da = new SqlDataAdapter(sqlStr, conn); // SqlDataAdapter da = new SqlDataAdapter(); //创建数据集对象(程序的临时数据库) DataSet ds = new DataSet(); DataTable dt1 = new DataTable(); //调用fill方法,填充数据集。 da.Fill(dt1); da.Fill(ds); //获得数据集的第一张表 DataTable dt = ds.Tables[0]; //循环数据表的每一行 for (int i = 0; i < dt.Rows.Count; i++) { DataRow dr = dt.Rows[i];//每一行付给dr Console.WriteLine(dr[0].ToString() + "___" + dr["b_title"].ToString()); } } }