ASP.NET连接数据库并获取数据方法总结

*连接对象的用法SqlConnection,SqlCommand,SqlDataAdapter
*数据访问方式的写法

1.获取数据:

//引用这两个命名空间 using System.Data.SqlClient; using System.Data; // 初始化连接对象 SqlConnection conn = new SqlConnection(); conn.ConnectionString = "User ID=sa;Initial Catalog=DataBaseName;Data Source= (local);Password=111111"; // 打开连接 if (conn.State == ConnectionState.Closed) { conn.Open(); } // 初始化命令 SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandType = CommandType.Text; cmd.CommandText = "sql语句"; // 用于执行数据插入、更新和删除的操作;返回被影响的行数。 int i = cmd.ExecuteNonQuery(); if(i>0){MessageBox.Show("操作成功");} // 用于查询最大值等只需返回一条数据情况下的操作;返回的是首行第一列的数据。 object obj = cmd.ExecuteScalar(); // 如果想获取数据集合的话我们经常使用到的是数据适配器 DataTable dt = new DataTable(); SqlDataAdapter adapter = new SqlDataAdapter(); adapter.SelectCommand = cmd; adapter.Fill(dt);

2.把数据绑定到数据控件

string str = "Data Source=.;Initial Catalog=GridView;User ID=sa;Password=111111"; string sql = "select * from UserName"; SqlConnection conn = new SqlConnection(str); //conn.Open(); 使用 SqlDataAdapter(数据适配器)不用写 //SqlCommand comm = new SqlCommand(sql, conn); //SqlDataAdapter dr = new SqlDataAdapter(comm); SqlDataAdapter dr = new SqlDataAdapter(sql,conn);//上面两句可以合并成这一 DataSet ds = new DataSet();//创建数据集; dr.Fill(ds); //填充数据集 this.GridView1.DataSource = ds; this.GridView1.DataBind();//讲数据源绑定到控件上, //conn.Close(); 关闭数据库连接 if (conn.State==ConnectionState.Open) //判断数据库连接状态,是否连接 { conn.Close(); }

3.使用SqlDataReader:

若要创建 SqlDataReader,必须调用 SqlCommand 对象的 ExecuteReader 方法,而不要直接使用构造函数。

string str = "Data Source=.;Initial Catalog=GridView;User ID=sa;Password=111111"; string sql = "select * from UserName"; SqlConnection conn = new SqlConnection(str); conn.Open(); SqlCommand comm = new SqlCommand(sql, conn); DataSet ds = new DataSet(); SqlDataReader dr = comm.ExecuteReader(); if (dr.Read()) { //下面两种都可以获得数据 //this.TextBox1.Text = dr.GetString(1); //this.TextBox2.Text = dr.GetInt32(3).ToString(); this.TextBox1.Text = dr.GetString(dr.GetOrdinal("Name")); this.TextBox2.Text = dr.GetInt32(dr.GetOrdinal("Age")).ToString(); } //循环输出 while (dr.Read()) { Response.Write(dr["Name"]); Response.Write(dr["Age"]); Response.Write("<br/>"); } dr.Close(); if (conn.State == ConnectionState.Open) { conn.Close(); }

SqlDataReader:提供一种从 SQL Server 数据库读取行的只进流的方式

补充:asp.net数据库连接web.config配置

SQL Server .NET Data Provider 连接字符串包含一个由一些属性名/值对组成的集合。每一个属性/值对都由分号隔开。

PropertyName1=Value1; PropertyName2=Value2; PropertyName3=Value3; .....

同样,连接字符串必须包含SQL Server实例名称:

复制代码 代码如下:

Data Source=ServerName;

使用本地的SQL Server(localhost),如果想要使用远程服务器运行,应该在示例对象中把正确的服务器赋给Data Source 属性。此外,还必须指定所支持的两种身份验证方法(即Windows身份验证和SQL Server身份验证)中的其中一种。Windows身份验证使用Windows登录用户身份连接数据库,而SQL身份验证要求显式地指定SQL Server用户ID和密码。要想使用Windows身份验证,必须在连接字符串中包括 Integrated Security 属性:

Data Source=ServerName; Integrated Security=True;

默认情况下,Integrated Security 属性为 False ,这意味着将禁用Windows身份验证。如果没有显式地把这个属性的值设置为True,连接将使用SQL Server身份验证,因此,必须提供SQL Server用户ID和密码。Integrated Security属性还能识别的其他值只有SSPI(Security Support Provider Interface,安全性支持提供者接口).在所有的Windows NT操作系统上,其中包括Windows NT 4.0、2000、XP,都支持值SSPI。它是使用Windows身份验证时可以使用的惟一接口,相当于把Integrated Security 属性值设置为True。

在Windows身份验证模式中,SQL Server使用Windows的安全子系统对用户连接进行有效性验证。即使显示地指定用户ID和密码,SQL Server也不检查连接字符串中的用户ID和密码。因为只有Windows NT、2000、XP支持SSPI,因此如果正使用的是这些操作系统,则只能使用Windows集成的安全策略去连接SQL Server。不论使用哪一个操作系统,当使用SQL Server身份验证时,必须在连接字符串中指定用户ID和密码:

Data Source=ServerName; User ID=donaldx; Password=unbreakable

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjwjdx.html