您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 项目/工程管理 > C#数据库编程常用代码示例
C#连接sql2000示范代码//定义连接字符串stringstrConnection=userid=sa;password=sd;initialcatalog=Tele_Sale;Server=168.10.10.86;ConnectTimeout=30;//用连接字符串创建一个数据库连接对象SqlConnectionobjConnection=newSqlConnection(strConnection);//使用数据库连接对象连接数据库objConnection.Open();//基于数据库连接对象创建一个sql命令SqlCommandSqlCmd=newSqlCommand(select*fromMissInfowhereMissID='234',objConnection);//创建一个数据阅读器并从sql命令中读出数据SqlDataReaderDataReader=SqlCmd.ExecuteReader();//使用数据阅读器中的字段内容DataReader.Read();MessageBox.Show(DataReader[0].ToString());MessageBox.Show(DataReader[1].ToString());MessageBox.Show(DataReader[2].ToString());//关闭数据库连接objConnection.Close();C#执行SQL命令(无返回值)示例代码(常用的Sql命令insert\delete\update都是无返回值的,本例以update为例)//定义连接字符串stringstrConnection=Server=168.10.10.86;initialcatalog=Tele_Sale;userid=sa;password=sd;ConnectTimeout=30;//用连接字符串创建一个数据库连接对象SqlConnectionobjConnection=newSqlConnection(strConnection);//使用数据库连接对象连接数据库objConnection.Open();//基于数据库连接对象创建一个sql命令SqlCommandSqlCmd=newSqlCommand(updateMissInfosetMissName='技术主管'whereMissID='234',objConnection);//执行不返回结果的sql命令SqlCmd.ExecuteNonQuery();//关闭数据库连接objConnection.Close();C#执行返回结果的SQL语句(select语句),并将结果显示到DataGridView说明:本例中的dgv是DataGridView控件,本你一并示范了在查询时把用户输入的工号拼接进了sql字符串。privatevoidbutton1_Click(objectsender,EventArgse){if(txt_User.Text.Trim()==){MessageBox.Show(请输入查询工号);txt_User.Focus();return;}//定义连接字符串//stringstrConnection=Server=168.10.10.86;initialcatalog=Tele_Sale;userid=sa;password=sd;ConnectTimeout=30;stringstrConnection=Server=168.10.10.86;database=Tele_Sale;uid=sa;pwd=sd;;//用连接字符串创建一个数据库连接对象SqlConnectionConn=newSqlConnection(strConnection);//打开数据库连接Conn.Open();//数据适配器(SqlDataadapter的作用是实现DataSet和DB之间的桥梁)SqlDataAdapterMyAdapter=newSqlDataAdapter(select*fromMissInfowhereMissID='+txt_User.Text.Trim()+',Conn);//SqlDataAdapterMyAdapter=newSqlDataAdapter(select*fromMissInfo,Conn);//创建数据记录集(可以把DataSet当成内存中的数据库,DataSet是不依赖于数据库的独立数据集合。)DataSetMyDataSet=newDataSet();//将数据适配器中的数据填充到数据记录集MyAdapter.Fill(MyDataSet,MyTable);//指定数据表格控件的数据源记录集dgv.DataSource=MyDataSet;//指定数据表格控件的数据源表名dgv.DataMember=MyTable;//关闭数据库连接Conn.Close();}C#将Excel作数据源(需在项目上“添加引用”—“MicrosoftOfficeExcel”)privatevoidbtn_Import_Click(objectsender,EventArgse){stringstrConn;//IMEX=1可把混合型作为文本型读取,避免null值strConn=Provider=Microsoft.Jet.OLEDB.4.0;DataSource=Test.xls;ExtendedProperties='Excel8.0;HDR=False;IMEX=1';OleDbConnectionOleConn=newOleDbConnection(strConn);OleConn.Open();Stringsql=SELECT*FROM[Sheet1$];OleDbDataAdapterOleDaExcel=newOleDbDataAdapter(sql,OleConn);DataSetds=newDataSet();OleDaExcel.Fill(ds,MyTable);//MyTable是自定义的别名dgv.DataSource=ds;dgv.DataMember=MyTable;OleConn.Close();}实战练习一:C#登录模块示例代码登录窗体详细设置(在InitializeComponent();处击右键,选择“转到定义”获取的代码):privatevoidInitializeComponent(){this.label1=newSystem.Windows.Forms.Label();this.label2=newSystem.Windows.Forms.Label();this.txt_User=newSystem.Windows.Forms.TextBox();this.txt_Pwd=newSystem.Windows.Forms.TextBox();this.btn_Login=newSystem.Windows.Forms.Button();this.btn_Exit=newSystem.Windows.Forms.Button();this.SuspendLayout();////label1//this.label1.AutoSize=true;this.label1.Location=newSystem.Drawing.Point(35,36);this.label1.Name=label1;this.label1.Size=newSystem.Drawing.Size(41,12);this.label1.TabIndex=0;this.label1.Text=工号:;////label2//this.label2.AutoSize=true;this.label2.Location=newSystem.Drawing.Point(35,90);this.label2.Name=label2;this.label2.Size=newSystem.Drawing.Size(41,12);this.label2.TabIndex=1;this.label2.Text=密码:;////txt_User//this.txt_User.Location=newSystem.Drawing.Point(91,33);this.txt_User.Name=txt_User;this.txt_User.Size=newSystem.Drawing.Size(100,21);this.txt_User.TabIndex=2;////txt_Pwd//this.txt_Pwd.Location=newSystem.Drawing.Point(91,87);this.txt_Pwd.Name=txt_Pwd;this.txt_Pwd.PasswordChar='*';this.txt_Pwd.Size=newSystem.Drawing.Size(100,21);this.txt_Pwd.TabIndex=3;////btn_Login//this.btn_Login.Location=newSystem.Drawing.Point(37,133);this.btn_Login.Name=btn_Login;this.btn_Login.Size=newSystem.Drawing.Size(61,23);this.btn_Login.TabIndex=4;this.btn_Login.Text=登录;this.btn_Login.UseVisualStyleBackColor=true;this.btn_Login.Click+=newSystem.EventHandler(this.btn_Login_Click);////btn_Exit//this.btn_Exit.Location=newSystem.Drawing.Point(137,133);this.btn_Exit.Name=btn_Exit;this.btn_Exit.Size=newSystem.Drawing.Size(54,23);this.btn_Exit.TabIndex=5;this.btn_Exit.Text=退出;this.btn_Exit.UseVisualStyleBackColor=true;this.btn_Exit.Click+=newSystem.EventHandler(this.btn_Exit_Click);////Form1//this.AutoScaleDimensions=newSystem.Drawing.SizeF(6F,12F);this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;this.ClientSize=newSystem.Drawing.Size(237,186);this.Controls.Add(this.btn_Exit);this.Controls.Add(this.btn_Login);this.Controls.Add(this.txt_Pwd);this.Controls.Add(this.txt_User);this.Controls.Add(this.label2);this.Controls.Add(this.label1);this.Name=Form1;this.StartPosition=System.Windows.Forms.FormStartPosition.CenterScreen;this.Text=系统登录;this.Load+=newSystem.EventHandler(this.Form1_Load);this.ResumeLayout(false);this.PerformLayout();}“登录”按钮源码:privatevoidbtn_Login_Click(objectsender,E
本文标题:C#数据库编程常用代码示例
链接地址:https://www.777doc.com/doc-3391472 .html