关键字:ADO.NET对象,ADO.NET控件,数据绑定和数据绑定控件。
使用ADO.NET开发数据库应用程序应遵循一下步骤:
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;
SqlCommand对象;OleDbCommand对象;OdbcCommand对象;OracleCommand对象。
CommandType = StoredProcedure;
CommandText = 存储过程名字;
Command.Parameters.Add("参数名",值类型,值);
Command.Parameters["参数名"].Value = "值";
CommandType = Text;
CommandText = SQL语句;
CommandType = TableDirect;
CommandText = 表名;
在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
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();
}
}
}
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);
}
No Data Now...
| ©2003-2008 Woody. Some Rights Reserved. Feed - Sitemap - Valid XHTML - Valid CSS - Creative Commons Powered by ABlog v3.2. Skin Reserved by Default | Processed in 0.031250 Seconds. 4 Queries | 浙ICP备07029590号 |