标签:
class Program
{
static void Main(string[] args)
{
//连接Oracle数据库========================================================
OracleConnectionStringBuilder constring00 = new OracleConnectionStringBuilder();
constring00.DataSource = "192.168.88.120/branch";
constring00.UserID = "testbranch";
constring00.Password = "testbranch";
Console.WriteLine(constring00.ToString());//输出连接字符串
OracleConnection con00 = new OracleConnection(constring00.ToString());
con00.Open();//打开数据库
con00.Dispose();//关闭数据库
//连接MySQL,使用“MySql.Data.dll”=========================================
MySqlConnectionStringBuilder constring01 = new MySqlConnectionStringBuilder();
constring01.Server = "localhost";
constring01.UserID = "root";
constring01.Password = "lizhiwei";
constring01.Database = "test";
constring01.Pooling = false;
constring01.Port = 3306;
Console.WriteLine(constring01.ToString());//输出连接字符串
MySqlConnection con01 = new MySqlConnection(constring01.ToString());
//con01.Open();//打开数据库
//con01.Dispose();//关闭数据库
//连接Access数据库(Access 2010级以上)====================================
OleDbConnectionStringBuilder constring02 = new OleDbConnectionStringBuilder();
/*Microsoft.Jet.OleDb.4.0(Access 2007级以下)
* Microsoft.ACE.OLEDB.12.0(Access 2010级以上)*/
constring02.Provider = "Microsoft.ACE.OLEDB.12.0";
/*Access.mdb(Access 2007级以下)
*Access.accdb(Access 2010级以上)*/
constring02.DataSource = @"d:\users\lizw\桌面\Access.accdb";
Console.WriteLine(constring02.ToString());//输出连接字符串
OleDbConnection con02 = new OleDbConnection(constring02.ToString());
con02.Open();//打开数据库
con02.Dispose();//关闭数据库
//连接SQLite数据库========================================================
SQLiteConnectionStringBuilder constring03 = new SQLiteConnectionStringBuilder();
constring03.DataSource = @"d:\users\lizw\桌面\SQLite.db";
Console.WriteLine(constring03.ToString());//输出连接字符串
SQLiteConnection con03 = new SQLiteConnection(constring03.ToString());
con03.Open();//打开数据库
con03.Dispose();//关闭数据库
Console.WriteLine("OK!");
Console.Read();
}
}
class Program
{
static void Main(string[] args)
{
//连接MySQL,使用“MySql.Data.dll”
MySqlConnectionStringBuilder constring01 = new MySqlConnectionStringBuilder();
constring01.Server = "localhost";
constring01.UserID = "root";
constring01.Password = "lizhiwei";
constring01.Database = "test";
constring01.Pooling = false;
constring01.Port = 3306;
MySqlConnection con01 = new MySqlConnection(constring01.ToString());
//第一种方式
try
{
con01.Open();//打开数据库
//操作数据库
}
catch (Exception e)
{
//异常处理
}
finally
{
con01.Dispose();//关闭数据库
}
//第二种方式
using (con01)
{
con01.Open();//打开数据库
//操作数据库
}
Console.WriteLine("OK!");
Console.Read();
}
}
class Program
{
static void Main(string[] args)
{
//连接MySQL,使用“MySql.Data.dll”
MySqlConnectionStringBuilder constring01 = new MySqlConnectionStringBuilder();
constring01.Server = "localhost";
constring01.UserID = "root";
constring01.Password = "lizhiwei";
constring01.Database = "test";
constring01.Pooling = false;
constring01.Port = 3306;
MySqlConnection con01 = new MySqlConnection(constring01.ToString());
con01.Open();//打开数据库
MySqlTransaction tra = con01.BeginTransaction();//开启事物
try
{
//操作数据库
tra.Commit();//提交操作
}
catch (Exception e)
{
tra.Rollback();//回滚数据
}
finally
{
con01.Dispose();//关闭数据库
}
Console.WriteLine("OK!");
Console.Read();
}
}
class Program
{
static void Main(string[] args)
{
//连接MySQL,使用“MySql.Data.dll”
MySqlConnectionStringBuilder constring01 = new MySqlConnectionStringBuilder();
constring01.Server = "localhost";
constring01.UserID = "root";
constring01.Password = "lizhiwei";
constring01.Database = "test";
constring01.Pooling = false;
constring01.Port = 3306;
MySqlConnection con01 = new MySqlConnection(constring01.ToString());
con01.Open();//打开数据库
MySqlTransaction tra = con01.BeginTransaction();//开始事物
string select = "delete from test1";
try
{
MySqlCommand cmd = new MySqlCommand(select, con01);
int count = cmd.ExecuteNonQuery();//执行命令返回受影响的数据记录数
tra.Commit();//提交
Console.WriteLine("删除记录数:" + count);
}
catch (Exception e)
{
tra.Rollback();//回滚
Console.WriteLine(e.Message);
}
finally
{
con01.Dispose();//关闭数据库
}
Console.WriteLine("OK!");
Console.Read();
}
}
class Program
{
static void Main(string[] args)
{
//连接MySQL,使用“MySql.Data.dll”
MySqlConnectionStringBuilder constring01 = new MySqlConnectionStringBuilder();
constring01.Server = "localhost";
constring01.UserID = "root";
constring01.Password = "lizhiwei";
constring01.Database = "test";
constring01.Pooling = false;
constring01.Port = 3306;
MySqlConnection con01 = new MySqlConnection(constring01.ToString());
con01.Open();//打开数据库
string select = "select name,age from test1";
try
{
MySqlCommand cmd = new MySqlCommand(select,con01);
MySqlDataReader reader = cmd.ExecuteReader();//执行命令返回结果集
while (reader.Read())
{
Console.WriteLine(reader[0]+"------"+reader[1]);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
con01.Dispose();//关闭数据库
}
Console.WriteLine("OK!");
Console.Read();
}
}
class Program
{
static void Main(string[] args)
{
//连接MySQL,使用“MySql.Data.dll”
MySqlConnectionStringBuilder constring01 = new MySqlConnectionStringBuilder();
constring01.Server = "localhost";
constring01.UserID = "root";
constring01.Password = "lizhiwei";
constring01.Database = "test";
constring01.Pooling = false;
constring01.Port = 3306;
MySqlConnection con01 = new MySqlConnection(constring01.ToString());
con01.Open();//打开数据库
string select = "select count(*) from test1";
try
{
MySqlCommand cmd = new MySqlCommand(select, con01);
object count = cmd.ExecuteScalar();//执行命令返回受影响的数据记录数
Console.WriteLine("记录数:" + count);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
con01.Dispose();//关闭数据库
}
Console.WriteLine("OK!");
Console.Read();
}
}
class Program
{
static void Main(string[] args)
{
//连接MySQL,使用“MySql.Data.dll”
MySqlConnectionStringBuilder constring01 = new MySqlConnectionStringBuilder();
constring01.Server = "localhost";
constring01.UserID = "root";
constring01.Password = "lizhiwei";
constring01.Database = "test";
constring01.Pooling = false;
constring01.Port = 3306;
MySqlConnection con01 = new MySqlConnection(constring01.ToString());
con01.Open();//打开数据库
MySqlTransaction tra = con01.BeginTransaction();//开始事物
string select = "INSERT INTO test1 (id,name,age) VALUES (@id,@name,@age);";
try
{
MySqlCommand cmd = new MySqlCommand(select, con01);
//设置参数类型
MySqlParameter id = new MySqlParameter("@id", MySqlDbType.Int32);
MySqlParameter name = new MySqlParameter("@name", MySqlDbType.VarChar,20);
MySqlParameter age = new MySqlParameter("@age", MySqlDbType.Int32);
//添加参数
cmd.Parameters.Add(id);
cmd.Parameters.Add(name);
cmd.Parameters.Add(age);
//为参数赋值
for (int i = 0; i < 100; i++)
{
id.Value = i;
name.Value = "李志伟" + i;
age.Value = i + 20;
cmd.ExecuteNonQuery();//插入数据
Console.WriteLine(i);
}
tra.Commit();//提交
}
catch (Exception e)
{
tra.Rollback();//回退
Console.WriteLine(e.Message);
}
finally
{
con01.Dispose();//关闭数据库
}
Console.WriteLine("OK!");
Console.Read();
}
}
class Program
{
static void Main(string[] args)
{
//连?接óMySQL,使?用?“°MySql.Data.dll”±
MySqlConnectionStringBuilder constring01 = new MySqlConnectionStringBuilder();
constring01.Server = "localhost";
constring01.UserID = "root";
constring01.Password = "lizhiwei";
constring01.Database = "test";
constring01.Pooling = false;
constring01.Port = 3306;
MySqlConnection con01 = new MySqlConnection(constring01.ToString());
con01.Open();//打洙?开a数簓据Y库a
using (con01)
{
string select = "select id,name,age from test1";
MySqlDataAdapter da = new MySqlDataAdapter(select, con01);
DataSet ds = new DataSet();
da.Fill(ds, "test1");
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
Console.WriteLine(ds.Tables[0].Rows[i]["name"] + " \t" + ds.Tables[0].Rows[i]["age"]);
}
}
Console.WriteLine("OK!");
Console.Read();
}
}
MySqlDataAdapter da = new MySqlDataAdapter(select, con01);
DataSet ds = new DataSet();
da.Fill(ds, "test1");
//手动写代码生成架构
DataTable table = new DataTable("test1");
table.Columns.Add(new DataColumn("id", typeof(int)));
table.Columns.Add(new DataColumn("name", typeof(string)));
table.Columns.Add(new DataColumn("age", typeof(int)));
DataSet ds = new DataSet();
//主表(老师信息表)
DataTable teacher = new DataTable("Teacher");
teacher.Columns.Add(new DataColumn("id", typeof(int)));
teacher.Columns.Add(new DataColumn("name", typeof(string)));
teacher.Columns.Add(new DataColumn("age", typeof(int)));
ds.Tables.Add(teacher);
//从表(学生信息表)
DataTable student = new DataTable("Student");
student.Columns.Add(new DataColumn("id", typeof(int)));
student.Columns.Add(new DataColumn("name", typeof(string)));
student.Columns.Add(new DataColumn("age", typeof(int)));
student.Columns.Add(new DataColumn("TeacherID", typeof(int)));//外键
ds.Tables.Add(student);
//建立关系
DataRelation dr = new DataRelation("TeacherStudent",//关系名
ds.Tables["Teacher"].Columns["id"],//主表的主键
ds.Tables["Student"].Columns["TeacherID"]);//从表的外键
ds.Relations.Add(dr);//增加关系
//填充数据
teacher.Rows.Add(1, "teacher1", 20);
student.Rows.Add(1, "student1", 18, 1);
student.Rows.Add(2, "student2", 17, 1);
student.Rows.Add(3, "student3", 16, 1);
//根据主表信息得到所有相应的从表信息
DataRow[] rows = ds.Tables["Teacher"].Rows[0].GetChildRows(dr);
foreach (DataRow temp in rows)
{
Console.WriteLine(temp["id"] + "\t" + temp["name"] + "\t" + temp["age"] + "\t" + temp["TeacherID"]);
}
DataSet ds = new DataSet();
//主表(老师信息表)
DataTable teacher = new DataTable("Teacher");
teacher.Columns.Add(new DataColumn("id", typeof(int)));
teacher.Columns.Add(new DataColumn("name", typeof(string)));
teacher.Columns.Add(new DataColumn("age", typeof(int)));
ds.Tables.Add(teacher);
//从表(学生信息表)
DataTable student = new DataTable("Student");
student.Columns.Add(new DataColumn("id", typeof(int)));
student.Columns.Add(new DataColumn("name", typeof(string)));
student.Columns.Add(new DataColumn("age", typeof(int)));
student.Columns.Add(new DataColumn("TeacherID", typeof(int)));//外猘键ü
ds.Tables.Add(student);
//主键约束
DataColumn[] pk = new DataColumn[1];
pk[0] = teacher.Columns["id"];
teacher.PrimaryKey = pk;
//外键约束
ForeignKeyConstraint fk = new ForeignKeyConstraint("FK_Teacher-Student",
teacher.Columns["id"],
student.Columns["TeacherID"]);
fk.UpdateRule = Rule.Cascade;//设置更新约束
fk.DeleteRule = Rule.SetNull;//设置删除约束
ds.Tables["Student"].Constraints.Add(fk);
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="Teacher"
targetNamespace="http://tempuri.org/XMLSchema1.xsd"
elementFormDefault="qualified"
xmlns="http://tempuri.org/XMLSchema1.xsd"
xmlns:mstns="http://tempuri.org/XMLSchema1.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="Teacher">
<xs:complexType>
<xs:sequence>
<xs:element name="id" msdata:ReadOnly="true" msdata:AutoIncrement="true" type="xs:int"/>
<xs:element name="name" type="xs:string"/>
<xs:element name="age" type="xs:int" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
DataSet ds = new DataSet();
ds.ReadXmlSchema(@".\XMLSchema.xsd");
MySqlDataAdapter da = new MySqlDataAdapter(select, con01);
DataSet ds = new DataSet();
da.Fill(ds, "test1");
DataSet ds = new DataSet();
ds.ReadXml(@".\MyData.xml");
class Program
{
static void Main(string[] args)
{
//连接MySQL,使用“MySql.Data.dll”
MySqlConnectionStringBuilder constring01 = new MySqlConnectionStringBuilder();
constring01.Server = "localhost";
constring01.UserID = "root";
constring01.Password = "lizhiwei";
constring01.Database = "test";
constring01.Pooling = false;
constring01.Port = 3306;
MySqlConnection con01 = new MySqlConnection(constring01.ToString());
con01.Open();//打开数据库
using (con01)
{
string sql = "select id,name,age from test1";
DataSet ds = new DataSet();
MySqlDataAdapter da = new MySqlDataAdapter(sql, con01);
MySqlCommandBuilder cb = new MySqlCommandBuilder(da);
da.Fill(ds, "test1");//填充数据集
foreach (DataRow temp in ds.Tables["test1"].Rows)
{
Console.WriteLine(temp["id"] + "\t" + temp["name"] + "\t" + temp["age"]);
}
Console.WriteLine("------------------------------------------------------");
DataRow row = ds.Tables["test1"].NewRow();//新增行
row["id"] = 2014;
row["name"] = "李志伟";
row["age"] = 21;
ds.Tables["test1"].Rows[0]["name"] = "李志伟2014";//更新现有行
ds.Tables["test1"].Rows[1].Delete();//删除行
da.Update(ds, "test1");//提交更新数据
ds.AcceptChanges();//更新数据集DataSet
foreach (DataRow temp in ds.Tables["test1"].Rows)
{
Console.WriteLine(temp["id"] + "\t" + temp["name"] + "\t" + temp["age"]);
}
}
Console.WriteLine("OK!");
Console.Read();
}
}
class Program
{
static void Main(string[] args)
{
//连接MySQL,使用“MySql.Data.dll”
MySqlConnectionStringBuilder constring01 = new MySqlConnectionStringBuilder();
constring01.Server = "localhost";
constring01.UserID = "root";
constring01.Password = "lizhiwei";
constring01.Database = "test";
constring01.Pooling = false;
constring01.Port = 3306;
MySqlConnection con01 = new MySqlConnection(constring01.ToString());
con01.Open();//打开数据库
using (con01)
{
string sql = "select id,name,age from test1";
DataSet ds = new DataSet();
MySqlDataAdapter da = new MySqlDataAdapter(sql, con01);
MySqlCommandBuilder cb = new MySqlCommandBuilder(da);
da.Fill(ds, "test1");
foreach (DataRow temp in ds.Tables["test1"].Rows)
{
Console.WriteLine(temp["id"] + "\t" + temp["name"] + "\t" + temp["age"]);
}
Console.WriteLine("------------------------------------------------------");
ds.WriteXml(@"D:\users\lizw\桌面\DataSet.xml");//得到XML数据
ds.WriteXmlSchema(@"D:\users\lizw\桌面\DataSetSchema.xml");//得到XML架构
}
Console.WriteLine("OK!");
Console.Read();
}
}
DataSet newds = ds.Copy();//复制当前DataSet的架构和数据
newds = ds.Clone();//只复制当前DataSet的架构
newds = ds.GetChanges();//只返回更改的数据
ds.GetChanges(DataRowState.Added);//只返回某些(这里是新增)更改的数据
ds.Merge(?);//合并数据
标签:
原文地址:http://www.cnblogs.com/LiZhiW/p/4316331.html