数据载入中......
公告 |
| 数据载入中...... |

|
|
| |
|
[ASP.NET] 数据库应用-Connection对象和Comman |
| 开开心心 发表于:2008-3-30 19:34:37 |
数据库应用-Connection对象和Command对象 关键字:ADO.NET对象,ADO.NET控件,数据绑定和数据绑定控件。
使用ADO.NET开发数据库应用程序应遵循一下步骤: 选择.NET Framework数据提供程序; SQL Server .NET Framework数据提供程序 System.Data.SqlClient >= Sql Server 7.0 OLEDB .NET Framework数据提供程序 System.Data.OleDb < Sql Server 7.0 或者OLE DB数据源 ODBC .NET Framework数据提供程序 System.Data.ODBC ODBC数据源(DSN) Oracle .NET Framework数据提供程序 System.Data.OracleClient Oracle数据源 使用Connection对象建立与数据源的连接; 使用Command对象完成对数据源的操作; 利用DataReader对象或DataSet对象缓存数据; 使用各种数据控件,如DataGrid控件显示数据。 Connection对象 使用连接
using System; using System.Data.OleDb; //引用添加 namespace UsingConnection { class TestDatabase { static void Main(string[] args) { try { //建立连接字符串 string myConStr = "Provider = Microsoft.Jet.OLEDB.4.0;"; myConStr += "Data Source = ADDRBOOK.mdb;"; //生成连接实例 OleDbConnection myCon = new OleDbConnection(myConStr); //建立连接 myCon.Open(); Console.WriteLine("Success! " + myCon.State.ToString()); //使用完连接,关闭连接 myCon.Close(); } catch(OleDbException oe) { Console.WriteLine(oe.Message); } } } }
Command对象 完成查询、插入、删除、更新,也可以是存储过程。 查询:
SELECT * FROM Employees;
插入: INSERT INTO Employees(LastName, FirstName) VALUES("Wang", " wu");
删除: DELETE FROM Employees WHERE employeeID = 9;
更新: UPDATE Employees SET FirstName = "gang" WHERE employeeID = 9;
Command对象对应不同的.NET Framework数据提供程序有 SqlCommand对象;OleDbCommand对象;OdbcCommand对象;OracleCommand对象。
存储过程: CommandType = StoredProcedure; CommandText = 存储过程名字; Command.Parameters.Add("参数名",值类型,值); Command.Parameters["参数名"].Value = "值";
SQL语句操作数据库:
CommandType = Text; CommandText = SQL语句;
直接访问数据表:
CommandType = TableDirect; CommandText = 表名;
主要方法: Execute, 执行所需要的操作; ExecuteReader 返回 DataReader对象,以数据流形式返回结果; ExecuteScalar 返回 结果集中的第一行的第一列,用于SQL聚合函数; ExecuteNonQuery 不返回结果,用于记录的插入、删除、更新操作。
在MS SQL Server Northwind数据库,创建存储过程EmployeeName,并用Command对象执行该存储过程。 创建存储过程EmployeeName,在查询分析器按F5执行下列代码。
CREATE PROCEDURE EmployeeName @City nvarchar(15) , @Count int OUTPUT AS SELECT firstname, lastname FROM employees WHERE city = @City SELECT @Count = @@rowcount GO
C#代码:
using System; //自己添加的数据操作引用 using System.Data.SqlClient; using System.Data; namespace UsingStoredProcedure { class UsingProcedure { static void Main(string[] args) { //注意你的电脑上应该装了MS SQL SERVER的Northwind数据库,将Server改为自己的服务器名 string Str = "Server = woodycmp; database = Northwind; user id = sa; password = sa;"; // string Str = "Persist Security Info = False; Initial Catalog = Northwind;"; // Str += "Data Source = woodycmp; Integrated Security = SSPI;user id = sa; password = sa"; SqlConnection Con = new SqlConnection(Str); SqlCommand Com = new SqlCommand(); Com.Connection = Con; //使用存储过程 Com.CommandType = CommandType.StoredProcedure; //给出存储过程的名字 Com.CommandText = "EmployeeName"; //给出存储过程的输入参数@City,并赋值 Com.Parameters.Add("@City", SqlDbType.NVarChar, 15); Com.Parameters["@City"].Value = "London"; //给出存储过程的输出参数@Count Com.Parameters.Add("@Count", SqlDbType.Int); //指明 @Count 参数作为输出参数 Com.Parameters["@Count"].Direction = ParameterDirection.Output; Con.Open(); SqlDataReader rd = Com.ExecuteReader(); Console.WriteLine("{0}\t{1}",rd.GetName(0),rd.GetName(1)); while(rd.Read()) { //输出员工信息 Console.WriteLine("{0}\t,\t{1}",rd.GetString(0),rd.GetString(1)); } rd.NextResult(); //获取输出参数@Count的值,并显示 Console.WriteLine("The count is {0}",Com.Parameters["@Count"].Value); rd.Close(); Con.Close(); } } }
使用SQL语句完成对Microsoft Access“地址薄”数据库的“家庭成员”数据表的插入、删除和更新操作。 界面设计:
C#关键代码: 窗体加载时候,显示第一条数据:
private void Display() { string myConStr = "Provider = Microsoft.Jet.OLEDB.4.0;"; myConStr += "Data Source = addrbook.mdb;"; OleDbConnection myCon = new OleDbConnection(myConStr); myCon.Open(); //创建Command对象 OleDbCommand myCom = new OleDbCommand(); //指定使用SQL语句 myCom.CommandType = CommandType.Text; //SQL语句是检索所有信息 string comStr = "SELECT 成员编号,地址编号,姓氏,名字,角色,发送贺卡"; comStr += " FROM 家庭成员"; myCom.CommandText = comStr; //使用myCon连接对象 myCom.Connection =myCon; //有Command对象的ExecuteReader方法生成OleDbDataReader对象 OleDbDataReader myReader = myCom.ExecuteReader(); //将获取的第一条记录在相应的文本框上显示出来 myReader.Read(); this.textBox1.Text = myReader.GetInt32(0).ToString(); this.textBox2.Text = myReader.GetInt32(1).ToString(); this.textBox3.Text = myReader.GetString(2); this.textBox4.Text = myReader.GetString(3); this.textBox5.Text = myReader.GetString(4); this.textBox6.Text = myReader.GetBoolean(5).ToString(); //关闭OleDbDataReader对象 myReader.Close(); //关闭Connection对象 myCon.Close(); }
操作数据库函数:
private void ExcuteSQL(string SQL) { string myConStr = "Provider = Microsoft.Jet.OLEDB.4.0;"; myConStr += "Data Source = addrbook.mdb;"; OleDbConnection myCon = new OleDbConnection(myConStr); myCon.Open(); OleDbCommand myCom = new OleDbCommand(); myCom.CommandType = CommandType.Text; myCom.CommandText = SQL; myCom.Connection = myCon; //由Command对象执行没有返回结果的SQL语句 myCom.ExecuteNonQuery(); myCon.Close(); }
添加数据:
private void btn_Insert_Click(object sender, System.EventArgs e) { //检查输入 if( this.textBox1.Text == "" && this.textBox2.Text == "" && this.textBox3.Text == "" && this.textBox4.Text =="" && this.textBox5.Text == "" && this.textBox6.Text == "") { MessageBox.Show("所有项都是必填项目,请填完后再单击添加按钮。","提示信息"); return; } //拼凑SQL语句 string InsertSQL = "insert into 家庭成员(地址编号,姓氏"; InsertSQL += ",名字,角色,发送贺卡)values( "; InsertSQL += this.textBox2.Text + ", '"; InsertSQL += this.textBox3.Text + "' , '"; InsertSQL += this.textBox4.Text + "' , '"; InsertSQL += this.textBox5.Text + "' , "; InsertSQL += this.textBox6.Text + ")"; //调用ExcuteSQL函数,执行插入操作 this.ExcuteSQL(InsertSQL); }
删除数据:
private void btn_delete_Click(object sender, System.EventArgs e) { if(this.textBox5.Text == "") { MessageBox.Show("必须给出角色"); this.textBox5.Focus(); } //拼凑SQL语句 string DeleteSQL = "delete from 家庭成员 where 角色 = '"; DeleteSQL += this.textBox5.Text + "'"; //调用ExcuteSQL函数,执行删除操作 this.ExcuteSQL(DeleteSQL); }
更新数据:
private void btn_modify_Click(object sender, System.EventArgs e) { if(this.textBox1.Text == "") { MessageBox.Show("必须给出成员编号"); this.textBox1.Focus(); } //拼凑SQL语句 string UpdateSQL = "update 家庭成员 set 名字 = '"; UpdateSQL += this.textBox4.Text; UpdateSQL += "' where 角色 = '"; UpdateSQL += this.textBox5.Text; UpdateSQL += " '"; //调用ExcuteSQL函数,执行更新数据库操作 this.ExcuteSQL(UpdateSQL); } |
|
|
数据载入中......
|