杂写

 

1: 保存点

  namespace ADO.SavePoint1201

{    class Program

{                static void Main(string[] args)

        {

            string strcon="Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=BookM;Data Source=06E2CCCA8D3D463";

            string sql = "select * from bank_money";

            SqlConnection conread = new SqlConnection(strcon);

            SqlConnection conshow = new SqlConnection(strcon);

            conread.Open();

            conshow.Open();

            SqlTransaction tranread = conread.BeginTransaction();

            SqlCommand cmdread = new SqlCommand();

            cmdread.Connection = conread;

            cmdread.Transaction = tranread;

 

            SqlTransaction transhow = conshow.BeginTransaction(System.Data.IsolationLevel.ReadUncommitted);        

            SqlCommand cmdshow = new SqlCommand(sql,conshow);

            //cmdshow.CommandText = sql;

            //cmdshow.Connection = conshow;

            cmdshow.Transaction = transhow;

 

            display("银行帐号信息表00000", cmdshow.ExecuteReader());

 

            cmdread.CommandText = "insert into Bank_Money values('小强','180','0755555538','新加坡')";

            cmdread.ExecuteNonQuery();

            tranread.Save("firstname");

            display("银行帐号信息表2222",cmdshow .ExecuteReader ());

 

            cmdread.CommandText = "insert into Bank_Money values('大秦','710','07225522149','泰国')";

            cmdread.ExecuteNonQuery();

            display("银行帐号信息表3333", cmdshow.ExecuteReader());

 

            cmdread.ExecuteNonQuery();

            tranread.Rollback("firstname");

            display("银行帐号信息表4444", cmdshow.ExecuteReader());

            //tranread.Commit();

            conread.Close();

            conshow.Close();

            Console.Read();

        }

        static void display(string msg, SqlDataReader  dr)

        {  

            if (dr.HasRows)

            {

                Console.WriteLine(msg);                Console.WriteLine("-------------------------------------------------------");

                while (dr.Read())

                {

                    Console.WriteLine("用户:"+dr[0]+"金额:"+dr[1]+"电话:"+dr [2]+"国籍:"+dr[3]);

                }

            }

            dr.Close();

        }

    }

}

 

2:分布式

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Data.SqlClient;

using System.IO;

using System.Transactions;

//transactions 的添加在项目---添加引用----找到Transactions;就可以了

namespace ADO.NET1201FeiBuShi

{

    public partial class FormFeiBuShi1201 : Form

    {

        public FormFeiBuShi1201()

        {

            InitializeComponent();

        }

        private string strconOne = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=BookM;Data Source=06E2CCCA8D3D463";

        private string strconTwo = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=BankMe;Data Source=06E2CCCA8D3D463";

 

        private void btnChangeBefore_Click(object sender, EventArgs e)

        {

            SqlDataAdapter da = new SqlDataAdapter("select * from Bank_Money", strconOne);

            DataSet ds = new DataSet();

            da.Fill(ds, "One");

 

            da = new SqlDataAdapter("select * from Bank_Momeytwo", strconTwo);

            da.Fill(ds, "Two");

 

            dgvtableone .DataSource =ds.Tables ["One"].DefaultView ;

            dgvtabletwo.DataSource = ds.Tables["Two"].DefaultView;

            label1.Text = da.SelectCommand.Connection.DataSource;

            label2.Text = da.SelectCommand.Connection.DataSource;

 

        }

 

        private void btnChangeAfter_Click(object sender, EventArgs e)

        {

            using (TransactionScope mytransaction = new TransactionScope())

            {    string sql="update Bank_Money set 金额=金额-"+txtboxMONEY .Text +" where 用户='张三'";

            SqlConnection strconA = new SqlConnection(strconOne);

                  strconA.Open();

                  SqlCommand cmdA = new SqlCommand();

                  cmdA.Connection = strconA;

                  cmdA.CommandText = sql;

                  cmdA.ExecuteNonQuery();

 

                  string sqlchang = "update Bank_Money set 金额=金额+" + txtboxMONEY.Text + " where用户='王宝强'";

                  SqlConnection strconb = new SqlConnection(strconTwo);

                  strconb.Open();

                  SqlCommand cmdB = new SqlCommand();

                  cmdB.CommandText = sqlchang;

                  cmdB.Connection = strconb;

                  cmdB.ExecuteNonQuery();

                  mytransaction.Complete();

                  MessageBox.Show("转账成功!");

            }

 

3:XML 的使用

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Data.SqlClient;

 

namespace ADOwithXML1210

{

    public partial class FormAboutXML : Form

    {

        public FormAboutXML()

        {

            InitializeComponent();

        }

        DataSet dsSource = new DataSet();

        private void btnLoad_Click(object sender, EventArgs e)

        {

            string strcon = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=AdventureWorks;Data Source=06E2CCCA8D3D463";

            string sql = "select * from HumanResources.Department";

            SqlDataAdapter da = new SqlDataAdapter(sql, strcon);

            da.Fill(dsSource, "department");

            dgvDataBase.DataSource = dsSource.Tables[0].DefaultView;

        }

 

        private void btnloadxml_Click(object sender, EventArgs e)

        {

            SaveFileDialog save = new SaveFileDialog();

            if (save.ShowDialog() == DialogResult.OK)

            {

                dsSource.WriteXmlSchema(save.FileName + "xsd");

                dsSource.WriteXml(save.FileName, XmlWriteMode.DiffGram);

            }

            MessageBox.Show("write xml file succed!");

        }

 

        private void btnshowChange_Click(object sender, EventArgs e)

        {

            DataSet ds = new DataSet();

            OpenFileDialog open = new OpenFileDialog();

            if (open.ShowDialog() == DialogResult.OK)

            {

                ds.ReadXmlSchema(open.FileName + "xsd");

                ds.ReadXml(open.FileName, XmlReadMode.DiffGram);

                dgvshowchange.DataSource = ds.GetChanges(DataRowState.Modified).Tables[0].DefaultView;

            }

        }

    }

}

4:适配器的使用

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

 

namespace User_SQldataAdapter_write_insert_11_03

{

    public partial class FormBanZiZhu1103 : Form

    {

        public FormBanZiZhu1103()

        {

            InitializeComponent();

        }

 

        private void btnExit_Click(object sender, EventArgs e)

        {

            Application.Exit();                退出的代码

        }

 

        private void FormBanZiZhu1103_Load(object sender, EventArgs e)

        {

            ClassSQLdataAdapter department = new ClassSQLdataAdapter();            //用类来实例化,可以确保能连接

            dgvTableMe.DataSource = department.getAlldepartment("Department");     从类中继承了方法 getAlldepartment

            dgvTableMe.DataMember = "Department";

        }

 

        //双击中 DoubleClick 事件

        private void FormBanZiZhu1103_DoubleClick(object sender, EventArgs e)

        {

            int RowId = Convert.ToInt32(dgvTableMe.SelectedRows[0].Cells[0].Value);   //RowId继承前面的 rowsNumber

            ClassSQLdataAdapter department = new ClassSQLdataAdapter();               //用类来实例化,可以确保能连接

            DataRow dr = department.getDepartmentByRowID(RowId);                    //RowId用来指定那一行

            txtName .Text =dr ("Name") .ToString ;

            txtName.Text = dr["Name"].ToString();

            txtGroupName.Text = dr["GroupName"].ToString();

            dateTimePicker1.Value = Convert.ToDateTime(dr["ModifiedDate"]);     //获取当前时间

            //txtGroupName .Text =dr ("name"). ToString ();

        }

 

        private void btnAddNew_Click(object sender, EventArgs e)

        {

            ClassSQLdataAdapter department = new ClassSQLdataAdapter();                            //用类来实例化,可以确保能连接

            department.insertDepartment(txtName.Text, txtGroupName.Text, dateTimePicker1.Value);    //注意insertDepartment

        }

 

        private void btnDelete_Click(object sender, EventArgs e)

        {

            int rowID=Convert .ToInt32 (dgvTableMe .SelectedRows [0].Cells [0].Value );

            ClassSQLdataAdapter department = new ClassSQLdataAdapter();                  //用类来实例化,可以确保能连接

            department.deletedepartment(rowID);                                           //RowId继承前面的 RowId 和和和和和和和和   deletedepartment

        }

 

        private void btnChange_Click(object sender, EventArgs e)

        {

            int rowID = Convert.ToInt32(dgvTableMe.SelectedRows[0].Cells[0].Value);

            ClassSQLdataAdapter department = new ClassSQLdataAdapter();                                 //用类来实例化,可以确保能连接

            department.changdepartment(txtName.Text, txtGroupName.Text, dateTimePicker1.Value, rowID);   //注意changdepartment和和     (txtName.Text, txtGroupName.Text, dateTimePicker1.Value, rowID

        }

 

 

    }

}

**************************************************************************************************************************************************************

using System;

using System.Collections.Generic;

using System.Text;

using System.Data.SqlClient;

using System.Data;

 

namespace User_SQldataAdapter_write_insert_11_03

{

    class ClassSQLdataAdapter

    {

        string ss;

        SqlDataAdapter da;

   

        //注意下面一行的名字要用上面类的明字 ClassSQLdataAdapter

        public ClassSQLdataAdapter ()

        {

            string sql = @"select row_number() over(order by [departmentID]) as Rowld,* from HumanResources.Department";

            ss = @"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=AdventureWorks;Data Source=06E2CCCA8D3D463";

            da = new SqlDataAdapter(sql, ss);     

        }

 

        public DataSet getAlldepartment(string TableName)   //这里的TableName和下面的有关,也和窗体中的有关

        {

            DataSet ds = new DataSet();

            da.Fill(ds, TableName);              //这里的TableName和窗体代码中的department.getAlldepartment("Department");

            return ds;                           //返回了东西,因此窗体代码中可以应用到它

        }

 

        public DataRow getDepartmentByRowID(int rowsNumber)

        {

            DataSet ds = new DataSet();                           //这里要注意 DataRow 和 rowsNumber

            da.Fill(ds, "Department");

            DataRow dr = ds.Tables[0].Rows[rowsNumber];           //这里的Tables[0]为什么要为0???????????

            return dr;                                           //返回了值,注意是dr

        }

        //添加记录的类

        public void insertDepartment(string Name, string GroupName, DateTime ModifiedDate)

        {

            DataSet ds = new DataSet();

            da.Fill(ds, "Department");

      

            DataRow dr = ds.Tables["Department"].NewRow();

            dr["Name"] = Name;

            dr["GroupName"] = GroupName;

            //这里的名字ModifiedDate要和数据库里那列的名字相同才能加进去

            dr["ModifiedDate"] = ModifiedDate;

                      

            ds.Tables["Department"].Rows.Add(dr );

            SqlCommandBuilder sb = new SqlCommandBuilder(da);

            da.InsertCommand = sb.GetInsertCommand();

            //注意下一行,Update把添加到数据库中

            da.Update(ds, "Department"); 

        }

 

        //删除记录的类

        # region

        public void deletedepartment(int rowID)

        {

            SqlCommandBuilder sb = new SqlCommandBuilder(da);    //实例化

            da.DeleteCommand = sb.GetDeleteCommand();            //注意 DeleteCommand和 GetDeleteCommand

            DataSet ds = new DataSet();

            da.Fill(ds, "Department");                           //填充表 Department

            ds.Tables[0].Rows[rowID - 1].Delete();               //注意Delete()

            //ds.Tables["Department"].Rows.Clear();           

            da.Update(ds, "Department"); 

            # region 网上来的 

            //int   pos=this.BindingContext[dSuserGrid1,"Tuser"].Position;   //记录所在位置

            //DataTable   mytable   =   dSuserGrid1.Tables[0];  

            //mytable.AcceptChanges();  

            待删除的列  

            //DataRowCollection   delrow   =   mytable.Rows;  

            删除列      

            //delrow[pos].Delete();  

            //sqlDataAdapter2.Update(dSuserGrid1,"Tuser");  

            //dSuserGrid1是用数据适配器生成的数据集对象,然后和datagrid帮定的。

            # endregion

        }

        # endregion 

        //更新数据库代码,其中设定了四个变量string Name, string GroupName, DateTime ModefiedDate,int rowID

        public void changdepartment(string Name, string GroupName, DateTime ModefiedDate,int rowID)

         {

             SqlCommandBuilder sb = new SqlCommandBuilder(da);             // SqlCommandBuilder 是一个很重要的系统关键字

             da.UpdateCommand = sb.GetUpdateCommand();                      //注意这一行的UpdateCommand和GetUpdateCommand   

             DataSet ds = new DataSet();

             da.Fill(ds, "Department");

             DataRow dr = ds.Tables[0].Rows[rowID - 1];                //后面没有GetChanges());和AcceptChanges(); 

            //右边的名字必须与数据库中的列名要相同

             dr["Name"] = Name;

             dr["GroupName"] = GroupName;

             dr["ModifiedDate"] = ModefiedDate;           

             da.Update(ds, "Department");

          }

    }

}

5:Datatable的使用

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Data.SqlClient;

 

namespace ADO_DataTable_11_10

{

    public partial class FormDataTable : Form

    {

        DataSet dsDepartment;

        DataTable tblDepartment;

        public FormDataTable()

        {

            InitializeComponent();

            dsDepartment = new DataSet();

            tblDepartment = new DataTable();

        } 

        private void FormDataTable_Load(object sender, EventArgs e)

        {

            string strcon = @"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=AdventureWorks;Data Source=06E2CCCA8D3D463";

            string sql = @"select * from HumanResources.Department";

            SqlDataAdapter da = new SqlDataAdapter(sql, strcon);

            da.Fill(dsDepartment, "Department"); 

            dgvGetDataBase.DataSource = dsDepartment;

            dgvGetDataBase.DataMember = "Department";

            //得到更新的信息

            tblDepartment = dsDepartment.Tables["Department"];

            tblDepartment.RowChanged += new DataRowChangeEventHandler(tblDepartment_RowChanged); 

        }

 

        void tblDepartment_RowChanged(object sender, DataRowChangeEventArgs e)

        {

            //throw new Exception("The method or operation is not implemented.");

            if (dsDepartment .HasChanges (DataRowState .Modified ))

            {

                DataSet dsDp = dsDepartment.GetChanges(DataRowState.Modified);

               dgvGetNew.DataSource = dsDp.Tables[0].DefaultView;

                //dgvGetNew.DataSource = dsDp.Tables[0].AcceptChanges;

            }

        }       

    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值