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

内容摘要
本文实例讲述了ASP.NET连接数据库并获取数据方法。分享给大家供大家参考,具体如下:
*连接对象的用法SqlConnection,SqlCommand,SqlDataAdapter

*数据访问方式的写法
1.获取数据
文章正文

本文实例讲述了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实例名称:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDataSqlClientSqlConnectionClassConnectionStringTopic.asp

连接字符串的书写应该熟练后应该没有什么问题,我是再看别人的程序,说实话有些东东还真不清楚。但连接不上的问题出现后,得解决呀。所以必须要弄懂这些关键字的意义,修改后再测试。

2、SQL Server的用户设置

问题一、采用连接字符串

复制代码 代码如下:
SqlConnection conn = new SqlConnection(" Uid=sa;Pwd=***;Initial Catalog=Northwind;Data Source=LocalHost;Connect Timeout=900");

错误:

用户"sa"登陆失败,没有可信任的Sql Server连接
查资料后找到解决方法:

原因:Sql Server的验证方式需要设置为Sql Server验证和Windows集成验证的混合方式,如果仅设置为后一种方式,就会出像上述问题

解决:运行Sql Server的企业管理器,点击服务器,在右键菜单中选择属性,选安全性,更改验证方式即可

问题二、采用连接字符串

复制代码 代码如下:
SqlConnection conn = new SqlConnection("Data Source=LocalHost;Integrated Security=SSPI;Initial Catalog=Northwind;");

错误:

用户"computername\IWAM_servername"登陆失败

原因:SQL Server的登陆用户中不包括IWAM_servername

解决方法:运行Sql Server的企业管理器,点击服务器,选安全性,选登陆,新建登陆中加入IWAM_servername,并配备相应的权限,如只能访问Northwind数据库,数据库的角色设置为public和db_owner。

3、关于连接的安全性

最好使用SSPI的集成安全方式连接数据库,而sa用户的方式连接会存在安全隐患,我认为主要是因为在安装SQL Server时,经常会为了访问的方便设置sa的密码为空,黑客一旦让sa成为管理员,就可以获得对系统的所有访问权限。所以为了数据库的安全,可以设置 SQL server的访问用户只能经过windows集成验证,设置sa的安全密码,加强数据库的安全性。当然设置为windows集成验证后,数据库的性能和访问的灵活性势必会受到影响,管理员可以针对每一个数据库设置不同的验证方式,而不必对SQL server设置成统一的方式。

希望本文所述对大家asp.net程序设计有所帮助。


代码注释

作者:喵哥笔记

IDC笔记

学的不仅是技术,更是梦想!

© 2020 IDC笔记 . | 备案号:辽ICP备18000516号