ADO.NET中5大对象的概述:
1---Connection对象:连接数据库。
(1)使用sa 登录
conn = new SqlConnection("Data Source=NEWLAND-PC;Initial Catalog=Srr1100U;User ID=sa;pwd=123456;"); conn.Open();
(2)使用windows本地登录
conn = new SqlConnection("Data Source=NEWLAND-PC;Initial Catalog=Image;Integrated Security=True;"); conn.Open();
2---Command对象:执行T-SQL语句。
(1)ExecuteNonQuery()方法,执行增删改命令。返回受影响的行数!一
if (comm.ExecuteNonQuery() > 0) { MessageBox.Show("保存成功!"); }
(2)ExecuteReader()方法,返回一个DataReader对象。一般用于string类型
GetConn(); string select = "select COUNT( distinct Data) as num from Srr1100U"; comm = new SqlCommand(select,conn); SqlDataReader MyData = comm.ExecuteReader(); MyData.Read(); this.txtHuman.Text = "人数:" + MyData["num"].ToString();
(3)ExecuteScalar()方法,返回首行首列。可以用于byte[]数组:
GetConn(); string select = "select Image from CameraImage where ImageName=‘" + DataId + "‘"; comm = new SqlCommand(select, conn); byte[] Mybyte = (byte[])comm.ExecuteScalar();
(4)SqlParameter[]参数,是DataSet的映射,可用来一次性更新大量的数据。
标准写法:
string select = "insert Message(CardData,Name,Money,Tel) values (@CardData,@Name,@Money,@Tel)"; SqlParameter[] MySqlParameter = new SqlParameter[] { new SqlParameter("@CardData",MyCardData), new SqlParameter("@Name",DataName), new SqlParameter("@Money",DataMoney), new SqlParameter("@Tel",DataTel) }; comm = new SqlCommand(select, conn); if (MySqlParameter != null && MySqlParameter.Length > 0) { foreach (SqlParameter p in MySqlParameter) { comm.Parameters.Add(p); } } if (comm.ExecuteNonQuery() > 0) { MessageBox.Show("写入成功"); }
简化写法:
string select = "insert Message(CardData,Name,Money,Tel) values (@CardData,@Name,@Money,@Tel)"; comm = new SqlCommand(select, conn); comm.Parameters.Add(new SqlParameter("@CardData", SqlDbType.NVarChar)); comm.Parameters.Add(new SqlParameter("@Name", SqlDbType.NVarChar)); comm.Parameters.Add(new SqlParameter("@Money", SqlDbType.NVarChar)); comm.Parameters.Add(new SqlParameter("@Tel", SqlDbType.NVarChar)); comm.Parameters[0].Value = MyCardData; comm.Parameters[1].Value = DataName; comm.Parameters[2].Value = DataMoney; comm.Parameters[3].Value = DataTel; if (comm.ExecuteNonQuery() > 0) { MessageBox.Show("写入成功"); }
3---DataAdapter对象:数据适配器,从数据库中检索数据,再填充到本地数据集中。同时,我们可以利用DataAdapter,再将数据反向从DataSet中更新回数据库。
(1) DataAdapter的Fill方法,用于使用DataAdapter的SelectCommand的结果来填充DataSet。
GetConn(); string select = "select * from CameraImage where ImageName=‘" + address + "‘"; MySqlDataAdapter = new SqlDataAdapter(select, conn); MyDataSet = new DataSet(); MySqlDataAdapter.Fill(MyDataSet); MemoryStream MyMemoryStream = new MemoryStream((byte[])MyDataSet.Tables[0].Rows[0][2]);
4---DataReader对象:是一个只向前的数据流。用于从数据源中检索只读数据集,并且必须有连接时才可以用。
CommandBehavior.CloseConnection属性,关闭时同时关闭conn连接
GetConn(); string Myselect = "select COUNT(CardData) as num from dbo.Message"; comm = new SqlCommand(Myselect, conn); SqlDataReader MySqlDataReader = comm.ExecuteReader(CommandBehavior.CloseConnection); if (MySqlDataReader .Read()) { MessageBox.Show(read2["num"].ToString()); } read.Close();
5---DataSet对象:是ADO.NET的核心,相当于本地数据库,它与数据库无关的,它和远程数据库连接的桥梁就是.NET数据提供程序。它是由多个表构成,每个表都是一个DataTable对象。
GetConn(); string select = "select * from CameraImage where ImageName=‘" + address + "‘"; MySqlDataAdapter = new SqlDataAdapter(select, conn); MyDataSet = new DataSet(); MySqlDataAdapter.Fill(MyDataSet); MemoryStream MyMemoryStream = new MemoryStream((byte[])MyDataSet.Tables[0].Rows[0][2]); //Tables表的标号,rows[行][列]