C#中MySQL函数用DATASET 和 MySqlDataAdapter 操作数据库

1.C#中调用MYSQL数据库时,我用的是MySQLDriverCS这个方法.

一般的查询可以方便执

            String connStr, commStr;
            DataSet ds;       //数据集
            BindingSource bs;  //数据绑定源,注意是全局的变量
            MySQLConnection myconn;
            MySQLDataAdapter myadp;//数据适配器            

            connStr = "Data Source=myshiyandb;Password=110810;User ID=root;Location=localhost;Port=3306;Extended Properties=";//数据库连接字符串
            //myconn = new MySQLConnection(new MySQLConnectionString("localhost", "myshiyandb", "root", "110810").AsString);
            myconn = new MySQLConnection(connStr);
            
            try
	    {
	        myconn.Open();//打开数据库
            }
	    catch(Exception e)
            {
	        MessageBox.Show(e.Message);
	     }
             //
	    ds = new DataSet();
            commStr = "select * from table1;";
            
            myadp = new MySQLDataAdapter(commStr , myconn); //适配器 
            myadp.Fill(ds,"table"); //将查询到数据填充到数据集
	    bs = new BindingSource();
	    bs.DataSource = ds.Tables["table"];
	    dataGridView1.DataSource = bs; //绑定DataGridView到DataSet
	    //shut
            try
            {
	        myconn.Close();
             }
	     catch(Exception e)
            {
	        MessageBox.Show(e.Message);
	    }

2.但是在通过这种方法,改一下其中的sql语句,虽然程序执行了,但是记录添加不到数据库中;解决办法

            connStr = "Data Source=myshiyandb;Password=110810;User ID=root;Location=localhost;Port=3306;Extended Properties=";//数据库连接字符串
            //myconn = new MySQLConnection(new MySQLConnectionString("localhost", "myshiyandb", "root", "110810").AsString);
            myconn = new MySQLConnection(connStr);

            try
            {
                myconn.Open();//打开数据库
            }
            catch (Exception e2)
            {
                MessageBox.Show(e2.Message);
            }

            string strInsert = null;
            strInsert = " insert into " + "table1" + " values "
                        + " ( "
                        + "'" + tbxLuruName.Text + "'" + ", "
                        + tbxLuruAge.Text 
                        + " ) ";
            //---
            commStr = strInsert;
            MySQLCommand cmd = new MySQLCommand(commStr, myconn);
            cmd.ExecuteNonQuery(); 
            //myadp = new MySQLDataAdapter(commStr, myconn); //适配器 
            
            try
            {
                myconn.Close();
            }
            catch (Exception e3)
            {
                MessageBox.Show(e3.Message);
            }


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
通信相关 1.C#HttpHelper,Httpwebrequest,请求时无视编码,无视证书,无视Cookie,网页抓取 主要是实现了HTTP协议的GET|POST请求时的无视编码,无视证书,设置代理,自动获取Cookie的功能。 访问数据库相关 1.SqlHelper类 使用C#实现对Sql数据库操作,执行存储过程,Sql语句,返回影响行数,返回DateTable,DataSet,DataReader,以及表集等方法。实现多个数据库的切换功能。功能强大,希望大家喜欢 2.一个操作Sql2005数据库的类(备份,还原,分离,附加,添加删除用户等操作) 这主要是为了让大家练手的,他使用C#代 码的方法实现了对数据的 备份,还原,分离,附加,添加和删除用户等操作 3.DbHelperMySQL类 数据访问抽象基础类 主要是访问Mysql数据库主要实现如下功能 1.得到最大值 2.是否存在 3.是否存在(基于MySqlParameter) 4.执行SQL语句,返回影响的记录数 5.执行MySql和Oracle滴混合事务,执行多条SQL语句,实现数据库事务。 6.执行带一个存储过程参数的的SQL语句。 7.执行带一个存储过程参数的的SQL语句。 8.向数据库里插入图像格式的字段(和上面情况类似的另一种实例) 9.执行一条计算查询结果语句,返回查询结果(object)。 10.执行查询语句,返回MySqlDataReader ( 注意:调用该方法后,一定要对MySqlDataReader进行Close ) 11.执行查询语句,返回DataSet,执行SQL语句,返回影响的记录数,执行多条SQL语句,实现数据库事务。 执行多条SQL语句,实现数据库事务。 12.执行多条SQL语句,实现数据库事务。 13.执行多条SQL语句,实现数据库事务。 14.执行一条计算查询结果语句,返回查询结果(object)。 15.执行查询语句,返回MySqlDataReader ( 注意:调用该方法后,一定要对MySqlDataReader进行Close ) 16.执行查询语句,返回DataSet等等。 复制代码 4.DbHelperOleDb类 1.数据访问基础类(基于OleDb)Access数据库, 2.得到最大值;是否存在;是否存在(基于OleDbParameter); 3.执行SQL语句,返回影响的记录数;执行多条SQL语句,实现数据库事务; 4.执行带一个存储过程参数的的SQL语句; 5.向数据库里插入图像格式的字段(和上面情况类似的另一种实例); 6.执行一条计算查询结果语句,返回查询结果(object); 7.执行查询语句,返回OleDbDataReader; 8.执行查询语句,返回DataSet; 9.执行SQL语句,返回影响的记录数; 10.执行多条SQL语句,实现数据库事务; 11.执行一条计算查询结果语句,返回查询结果(object); 12.执行查询语句,返回OleDbDataReader;执行查询语句,返回DataSet; 复制代码 5.DbHelperOra类 1.数据访问基础类(基于Oracle),主要是用来访问Oracle数据库的。 2.得到最大值;是否存在;是否存在(基于OracleParameter ); 3.执行SQL语句,返回影响的记录数; 4.执行多条SQL语句,实现数据库事务; 5.执行带一个存储过程参数的的SQL语句; 6.向数据库里插入图像格式的字段(和上面情况类似的另一种实例); 7.执行一条计算查询结果语句,返回查询结果(object); 8.执行查询语句,返回OracleDataReader ( 注意:调用该方法后,一定要对SqlDataReader进行Close ); 9.执行查询语句,返回DataSet; 10.执行SQL语句,返回影响的记录数; 11.执行多条SQL语句,实现数据库事务; 12.执行一条计算查询结果语句,返回查询结果(object); 13.执行查询语句,返回OracleDataReader ( 注意:调用该方法后,一定要对SqlDataReader进行Close ); 14.执行查询语句,返回DataSet; 15.执行存储过程 返回SqlDataReader ( 注意:调用该方法后,一定要对SqlDataReader进行Close ); 16.执行存储过程;构建 OracleCommand 对象(用来返回一个结果集,而不是一个整数值); 17.执行存储过程,返回影响的行数; 18.创建 OracleCommand 对象实例(用来返回一个整数值) 复制代码 6.DbHelperSQLite类 [code=csharp] 1.数据访问基础类(基于SQLite),主要是用来访问SQLite数据库的。 2.得到最大值;是否存在;是否存在(基于SQLiteParameter); 3. 执行SQL语句,返回影响的记录数 4.执行多条SQL语句,实现数据库事务。 5.执行带一个存储过程参数的的SQL语句。 6.向数据库里插入图像格式的字段(和上面情况类似的另一种实例) 7.执行一条计算查询结果语句,返回查询结果(object)。 8.执行查询语句,返回SQLiteDataReader 9.执行查询语句,返回DataSet 10.执行SQL语句,返回影响的记录数 11. 执行多条SQL语句,实现数据库事务。 12. 执行一条计算查询结果语句,返回查询结果(object)。 13.执行查询语句,返回SQLiteDataReader 14.执行查询语句还参数,返回DataSet[/code] 7.DbHelperSQLP类 8.DbHelperSQL类 9.OracleHelper类 帮助类 1.最新的Functions 类 这里面实现了很多的帮助方法,比如正则验证,加密,解密,MD5加密,字符串的处理等操作。 2.最新的 PageValidate 类 主要是实现了验证,是否为空,是否为数字等。 3.JavascriptHelp 帮助输出简单的JS代码 4.最新的皮肤帮助类 UI_Misc_Helper JavaScript|Jquery相关 1.jQuery.cookie帮助类 访问系统相关 1.C#计算机信息类ComputerInfo 实现了计算机的信息获取功能,计算机名,IP,硬盘信息,网卡信息,操作系统信息等 2.Api_Win32_Mac类工具包 一个Win32的Api包,实现了大部分的Api操作帮助方法 3.在c#程序放音乐的帮助类 使用C#播放音乐的帮助类,只需要调用方法就可以放音乐了 GDI+相关,图像相关 1.生成缩略图的类文件SmallImage 一个把图片生成缩略图的类,可以设置大小,非常好用,希望大家喜欢哦。 C#基础类库 1.Chart图形 Assistant创建显示图像的标签和文件 OWCChart统计图的封装类 2.Cookie&Session&Cache缓存帮助类 CacheHelper C#操作缓存的帮助类,实现了怎么设置缓存,怎么取缓存,怎么清理缓存等方法,只需要调用方法就可以实现 CookieHelper C#操作Cookie的帮助类,添加Cookie,删除Cookie,修改Cookie,清理Cookie SessionHelper C#关于Session的操作,获取Session,设置Session,删除Session使用方便,只需要调用方法就可以了 SessionHelper2 C#关于Session的一些高级操作,比如取Session对象,取Session数据等等 3.CSV文件转换 CsvHelper CSV文件导入DataTable和DataTable导出到Csv文件等操作 4.DEncrypt 加密/解密帮助类 DEncrypt C#DEncrypt加密/DEncrypt解密帮助类 ,多种方式,可以设置Key DESEncrypt C#DESEncrypt加密/DESEncrypt解密帮助类 ,多种方式,可以设置Key Encrypt C#Encrypt--Encrypt加密/Encrypt解密/附加有MD5加密,个人感觉很不错的一个加密类 HashEncode 哈希加密帮助类,得到随机哈希加密字符串,随机哈希数字加密等 MySecurity MySecurity--Security安全加密/Security Base64/Security文件加密,以及一些常用的操作方法 RSACryption RSACryption--RSA加密/RSA解密字符串 RSA加密应用最多是银行接口,这里的方法可以直接使用哦 5.FTP操作类 FTPClient   FTPClient--FTP操作帮助类,FTP上传,FTP下载,FTP文件操作,FTP目录操作 FTPHelper FTPHelper-FTP帮助类,FTP常用操作方法,添加文件,删除文件等 FTPOperater FTP操作帮助类,方法比较多,比较实用 6.JS操作类 JsHelper JsHelper--Javascript操作帮助类,输出各种JS方法,方便不懂JS的人使用,减少代码量 7.JSON 转化类 ConvertJson List转成Json|对象转成Json|集合转成Json|DataSet转成Json|DataTable转成Json|DataReader转成Json等 8.Mime MediaTypes 电子邮件类型帮助类,规定是以Xml,HTML还是文本方式发送邮件 MimeEntity Mime实体帮助类 MimeHeaders mime的Header帮助类 MimeReader mime读取帮助类 QuotedPrintableEncoding mimeEncoding帮助类 9.PDF 转化类 PDFOperation PDFOperation--C#PDF文件操作帮助类 类主要功能有1.构造函数2.私有字段3.设置字体4.设置页面大小 5.实例化文档6.打开文档对象7.关闭打开的文档8.添加段落9.添加图片10.添加链接、点 等功能 10.ResourceManager 操作类 AppMessage app消息格式化类,返加字符串帮助类 ResourceManager C#一个操作Resource的帮助类 ResourceManagerWrapper Resources 操作Resources的帮助类,使用Api的方式 Sample.xml 11.XML操作类 XmlHelper 操作Xml文档的帮助类,主要是添加,删除,修改,查询节点的操作操作后进行保存的功能。 XMLProcess 操作Xml文档的帮助类,主要是添加,删除,修改,查询节点的操作的功能。 12.弹出消息类 MessageBox JS弹出信息帮助类 ShowMessageBox 相对于MessageBox更丰富的提示类 13.导出Excel 操作DataToExcel 从“Excel导出数据的帮助类 ExcelHelper 导出到文件,导出一部分集合,从DataTable操作等 ExportExcel 主要功能如下1.将整个网页导出来Excel 2.将GridView数据导出Excel GridViewExport 主要功能:将整GridView的数据导出到Excel关增加一个效果线做美化 14.分词辅助类 SegList C#SegList分词辅助类,帮助类 15.汉字转拼音 EcanConvertToCh C#将汉字转成拼音 PinYin 取汉字拼音的首字母,只要你输入一个汉字,或者是多个汉字就会取出相应的道字母,主要是方便查询使用的 复制代码 16.配置文件操作类 ConfigHelper 1.根据Key取Value值 2.根据Key修改Value 3.添加新的Key ,Value键值对 4.根据Key删除项 复制代码 17.日历 CNDate 1.传回公历y年m月的总天数 2.根据日期值获得周一的日期 3.获取农历 复制代码 18.上传下载 DownLoadHelper 输出硬盘文件,提供下载 支持大文件、续传、速度限制、资源占用小 复制代码 FileDown 1.参数为虚拟路径 2.获取物理地址 3.普通下载 4.分块下载 5.输出硬盘文件,提供下载 支持大文件、续传、速度限制、资源占用小 复制代码 FileUp 1.把上传的文件转换为字节数组 2.流转化为字节数组 2.上传文件根据FileUpload控件上传 3.把Byte流上传到指定目录并保存为文件 复制代码 UpLoadFiles 页面专用类 19.时间操作类 DateFormat 返回每月的第一天和最后一天 复制代码 TimeHelper 1.将时间格式化成 年月日 的形式,如果时间为null,返回当前系统时间 2.将时间格式化成 时分秒 的形式,如果时间为null,返回当前系统时间 3.把秒转换成分钟 4.返回某年某月最后一天 5.返回时间差 6.获得两个日期的间隔 7.格式化日期时间 8.得到随机日期 复制代码 20.视频转换类 VideoConvert 1.获取文件的名字 2.获取文件扩展名 3.获取文件类型 4.视频格式转为Flv 5.生成Flv视频的缩略图 6.转换文件并保存在指定文件夹下 7.转换文件并保存在指定文件夹下 8.运行mencoder的视频解码器转换 复制代码 21.随机数类 BaseRandom 1.产生随机字符 2.产生随机数 3.在一定范围内产生随机数 复制代码 RandomHelper 1.生成一个指定范围的随机整数,该随机数范围包括最小值,但不包括最大值 2.生成一个0.0到1.0的随机小数 3.对一个数组进行随机排序 4. 一:随机生成不重复数字字符串 5.方法二:随机生成字符串(数字和字母混和) 6.从字符串里随机得到,规定个数的字符串. 复制代码 22.条形码 BarCodeToHTML 本类是个条码生成类,大家可根据需要自己设置,非常好用 23.图片 ImageClass 主要功能有:缩略图片,图片水印,文字水印,调整光暗,反色处理,浮雕处理,拉伸处理,左右翻转,上下翻转, 压缩图片,图片灰度化,转换为黑白图片,获取图片的各帧 ImageDown 主要功能,把图片下载到本地 ImageUpload 图片上传并进行缩略图处理 24.网络 NetHelper 25.文件操作类 DirFileHelper FileOperateHelper INIFile 26.序列化 Serialize 序列化帮助类,还有例子 SerializeHelper 序列化帮助类,Xml序列化,Json序列化,SoapFormatter序列化,BinaryFormatter序列化 27.压缩解压缩 SharpZip 28.验证码 YZMHelper Captcha 验证码类,一个很个性的验证码类 29.页面辅助类 HTMLHelper UploadEventArgs JavaScriptPlus PageHelper 30.邮件 MailHelper MailPoper MailSender SmtpServerHelper 31.邮件2 ConnectCommand ConnectResponse DeleCommand ListCommand ListResponse MailHeaders MailMessageEx NoopCommand PassCommand Pop3Client Pop3Command Pop3Commands Pop3Exception Pop3ListItem Pop3Response Pop3Responses Pop3State QuitCommand RetrCommand RetrResponse RsetCommand Stat StatCommand StatResponse TopCommand UserCommand 32.正则表达式 RegexHelper 33.字符串 StringHelper 34.其它 BasePage BindDataControl ConvertHelper DataCache FormulaExpress GridViewHelper IpHelper MediaHandler PageValidate PicDeal QueryString Rmb StringPlus SysHelper Tools UrlOper Utility ValidateImg WebSitePathHelper
/* API精灵 FOR C# 开始设计日期 2004.03.06 设计目的:简单快速对C#使用的API函数进行查询,并给出调用代码 设计进度: 2004.03.09 完成对的查询功能,包括 代码调用,文注释,所需的DLL库,与C#函数对应关系 2004.03.10 0:48:52 完成了用StringBuilder数组对原ComboBox的替换,可以使程序不用从新读取数据库就可以刷新修改后的信息! 2004.03.10 18:00:00 完成了用ArrayList对StringBuilder数组的替换节省2M内存 2004.03.11 21:10:15 完成滚动字幕的设置,启用了一个TIMER控件,然后设置时间,删除字符串的第一个字母已达到滚动效果! 2004.03.11 22:02:00 改正更新时出现空值出错问题,新填函数isnull 2004.03.12 13:22:08 完成关键字高亮显示 高亮显示函数 mykeywords 2004.03.12 22:08:20 加强了高亮显示函数 mykeywords的功能,使其能识别不同的关键字并显示不同的颜色 2004.03.14 01:40:00 完成对CONST的查询,并且增加了 mykeywords1函数,使其关键字显示性能提高 2004.03.14 13:12:00 添加了提示信息,提示信息设置在函数 mytips() 2004.03.15 21:51:20 更改数据库和WINAPI.TXT路径为程序运行路径 2004.03.15 22:31:50 添加了鼠标右键信息 2004.03.15 23.23:30 添加了数据库密码 2004.03.16 23:24:30 添加了版权信息以及相应提示 */ using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Data.OleDb; using System.Runtime.InteropServices; using System.IO; namespace API精灵 { /// /// Form1 的摘要说明。 /// 这个版本没有使用oleDbDataAdapterDataSet对数据进行存取,而是使用的OleDbCommand +OleDbDataReader 的形式。 /// 主要是想试验一下不用oleDbDataAdapterDataSet的数据存取速度。 /// public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Button button1; private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.GroupBox groupBox2; private System.Windows.Forms.GroupBox groupBox3; private System.Windows.Forms.Button button4; private System.Windows.Forms.TextBox mysearch; private System.Windows.Forms.ListBox tiplist; private System.Windows.Forms.ComboBox select_type; private System.Windows.Forms.TextBox dlltext; /// /// 必需的设计器变量。 /// //自定义变量 private ArrayList fundll = new ArrayList();//保存读取出来的DLL内容 private ArrayList funtips = new ArrayList();//保存读取出来的文提示信息 private ArrayList funcode = new ArrayList();//保存读取出来的C#调用代码 private ArrayList funmat = new ArrayList();//保存读取出来的C#对应函数 private ArrayList funwin9x = new ArrayList();//保存读取出来的WIN9X private ArrayList funwin2k = new ArrayList();//保存读取出来的WIN2K private int nowselect = 0; private string oldscoll_text; private int nowtypeselect = 0; private string nowpath = @System.Environment.CurrentDirectory+@"\"; private string dbpassword = "ling_feng_work"; public string myConnstr; public OleDbConnection myconn ;//创建一个新连接 private string mysql ;//查询语句 private string sql_update; private System.Windows.Forms.RichTextBox tipsmemo; private System.Windows.Forms.TextBox mat_text; private OleDbCommand mycommand = new OleDbCommand(); private System.Windows.Forms.RichTextBox codememo; private System.Windows.Forms.GroupBox groupBox4; private System.Windows.Forms.CheckBox win9x; private System.Windows.Forms.GroupBox groupBox5; private System.Windows.Forms.CheckBox win2k; private System.Windows.Forms.CheckBox e_add; private System.Windows.Forms.CheckBox e_modify; private System.Windows.Forms.Button b_modify; private System.Windows.Forms.Button b_add; private System.Windows.Forms.Timer timer1; private System.Windows.Forms.ToolTip toolTip1; private System.Windows.Forms.ContextMenu mypop; private System.Windows.Forms.MenuItem menuItem1; private System.ComponentModel.IContainer components; public Form1() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); // // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // } /// /// 清理所有正在使用的资源。 /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows 窗体设计器生成的代码 /// /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// private void InitializeComponent() { this.components = new System.ComponentModel.Container(); System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1)); this.button1 = new System.Windows.Forms.Button(); this.groupBox1 = new System.Windows.Forms.GroupBox(); this.tiplist = new System.Windows.Forms.ListBox(); this.select_type = new System.Windows.Forms.ComboBox(); this.mat_text = new System.Windows.Forms.TextBox(); this.mysearch = new System.Windows.Forms.TextBox(); this.dlltext = new System.Windows.Forms.TextBox(); this.groupBox2 = new System.Windows.Forms.GroupBox(); this.tipsmemo = new System.Windows.Forms.RichTextBox(); this.mypop = new System.Windows.Forms.ContextMenu(); this.menuItem1 = new System.Windows.Forms.MenuItem(); this.groupBox3 = new System.Windows.Forms.GroupBox(); this.codememo = new System.Windows.Forms.RichTextBox(); this.b_modify = new System.Windows.Forms.Button(); this.b_add = new System.Windows.Forms.Button(); this.button4 = new System.Windows.Forms.Button(); this.groupBox4 = new System.Windows.Forms.GroupBox(); this.win2k = new System.Windows.Forms.CheckBox(); this.win9x = new System.Windows.Forms.CheckBox(); this.groupBox5 = new System.Windows.Forms.GroupBox(); this.e_add = new System.Windows.Forms.CheckBox(); this.e_modify = new System.Windows.Forms.CheckBox(); this.timer1 = new System.Windows.Forms.Timer(this.components); this.toolTip1 = new System.Windows.Forms.ToolTip(this.components); this.groupBox1.SuspendLayout(); this.groupBox2.SuspendLayout(); this.groupBox3.SuspendLayout(); this.groupBox4.SuspendLayout(); this.groupBox5.SuspendLayout(); this.SuspendLayout(); // // button1 // this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat; this.button1.Location = new System.Drawing.Point(224, 395); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 24); this.button1.TabIndex = 10; this.button1.Text = "关 于"; this.button1.Click += new System.EventHandler(this.button1_Click); // // groupBox1 // this.groupBox1.Controls.Add(this.tiplist); this.groupBox1.Controls.Add(this.select_type); this.groupBox1.Controls.Add(this.mat_text); this.groupBox1.Controls.Add(this.mysearch); this.groupBox1.Controls.Add(this.dlltext); this.groupBox1.Location = new System.Drawing.Point(8, 8); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(200, 168); this.groupBox1.TabIndex = 1; this.groupBox1.TabStop = false; this.groupBox1.Text = "API查询"; // // tiplist // this.tiplist.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.tiplist.ItemHeight = 12; this.tiplist.Location = new System.Drawing.Point(8, 43); this.tiplist.Name = "tiplist"; this.tiplist.Size = new System.Drawing.Size(184, 110); this.tiplist.TabIndex = 1; this.tiplist.Visible = false; this.tiplist.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tiplist_KeyDown); this.tiplist.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.tiplist_KeyPress); this.tiplist.DoubleClick += new System.EventHandler(this.tiplist_DoubleClick); this.tiplist.MouseUp += new System.Windows.Forms.MouseEventHandler(this.tiplist_MouseUp); this.tiplist.MouseLeave += new System.EventHandler(this.tiplist_MouseLeave); this.tiplist.SelectedIndexChanged += new System.EventHandler(this.tiplist_SelectedIndexChanged); // // select_type // this.select_type.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.select_type.Items.AddRange(new object[] { "API函数查询", "常量定义查询"}); this.select_type.Location = new System.Drawing.Point(8, 61); this.select_type.Name = "select_type"; this.select_type.Size = new System.Drawing.Size(184, 20); this.select_type.TabIndex = 2; this.select_type.SelectedIndexChanged += new System.EventHandler(this.select_type_SelectedIndexChanged); // // mat_text // this.mat_text.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.mat_text.Location = new System.Drawing.Point(8, 134); this.mat_text.Name = "mat_text"; this.mat_text.Size = new System.Drawing.Size(184, 21); this.mat_text.TabIndex = 4; this.mat_text.Text = "C#对应函数:"; this.mat_text.MouseDown += new System.Windows.Forms.MouseEventHandler(this.mat_text_MouseDown); // // mysearch // this.mysearch.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.mysearch.Location = new System.Drawing.Point(8, 24); this.mysearch.Name = "mysearch"; this.mysearch.Size = new System.Drawing.Size(184, 21); this.mysearch.TabIndex = 0; this.mysearch.Text = ""; this.mysearch.KeyDown += new System.Windows.Forms.KeyEventHandler(this.mysearch_KeyDown); this.mysearch.MouseDown += new System.Windows.Forms.MouseEventHandler(this.mysearch_MouseDown); this.mysearch.TextChanged += new System.EventHandler(this.mysearch_TextChanged); // // dlltext // this.dlltext.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.dlltext.Location = new System.Drawing.Point(8, 97); this.dlltext.Name = "dlltext"; this.dlltext.Size = new System.Drawing.Size(184, 21); this.dlltext.TabIndex = 3; this.dlltext.Text = ""; this.dlltext.TextChanged += new System.EventHandler(this.dlltext_TextChanged); // // groupBox2 // this.groupBox2.Controls.Add(this.tipsmemo); this.groupBox2.Location = new System.Drawing.Point(216, 8); this.groupBox2.Name = "groupBox2"; this.groupBox2.Size = new System.Drawing.Size(200, 168); this.groupBox2.TabIndex = 2; this.groupBox2.TabStop = false; this.groupBox2.Text = "函数注释"; // // tipsmemo // this.tipsmemo.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.tipsmemo.ContextMenu = this.mypop; this.tipsmemo.Location = new System.Drawing.Point(8, 16); this.tipsmemo.Name = "tipsmemo"; this.tipsmemo.Size = new System.Drawing.Size(184, 144); this.tipsmemo.TabIndex = 0; this.tipsmemo.Text = ""; this.tipsmemo.MouseEnter += new System.EventHandler(this.richTextBox1_MouseEnter); // // mypop // this.mypop.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.menuItem1}); // // menuItem1 // this.menuItem1.Index = 0; this.menuItem1.Text = "复制信息"; this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click); // // groupBox3 // this.groupBox3.Controls.Add(this.codememo); this.groupBox3.Location = new System.Drawing.Point(8, 221); this.groupBox3.Name = "groupBox3"; this.groupBox3.Size = new System.Drawing.Size(408, 168); this.groupBox3.TabIndex = 3; this.groupBox3.TabStop = false; this.groupBox3.Text = "代码调用"; // // codememo // this.codememo.ContextMenu = this.mypop; this.codememo.Location = new System.Drawing.Point(8, 16); this.codememo.Name = "codememo"; this.codememo.Size = new System.Drawing.Size(392, 144); this.codememo.TabIndex = 0; this.codememo.Text = ""; this.codememo.TextChanged += new System.EventHandler(this.codememo_TextChanged); // // b_modify // this.b_modify.Enabled = false; this.b_modify.FlatStyle = System.Windows.Forms.FlatStyle.Flat; this.b_modify.Location = new System.Drawing.Point(120, 395); this.b_modify.Name = "b_modify"; this.b_modify.Size = new System.Drawing.Size(75, 24); this.b_modify.TabIndex = 4; this.b_modify.Text = "修改信息"; this.b_modify.Click += new System.EventHandler(this.button2_Click); // // b_add // this.b_add.Enabled = false; this.b_add.FlatStyle = System.Windows.Forms.FlatStyle.Flat; this.b_add.Location = new System.Drawing.Point(16, 395); this.b_add.Name = "b_add"; this.b_add.Size = new System.Drawing.Size(75, 24); this.b_add.TabIndex = 5; this.b_add.Text = "添加新项"; // // button4 // this.button4.FlatStyle = System.Windows.Forms.FlatStyle.Flat; this.button4.Location = new System.Drawing.Point(328, 395); this.button4.Name = "button4"; this.button4.Size = new System.Drawing.Size(75, 24); this.button4.TabIndex = 6; this.button4.Text = "退 出"; this.button4.Click += new System.EventHandler(this.button4_Click); // // groupBox4 // this.groupBox4.Controls.Add(this.win2k); this.groupBox4.Controls.Add(this.win9x); this.groupBox4.Location = new System.Drawing.Point(8, 176); this.groupBox4.Name = "groupBox4"; this.groupBox4.Size = new System.Drawing.Size(200, 40); this.groupBox4.TabIndex = 11; this.groupBox4.TabStop = false; // // win2k // this.win2k.Location = new System.Drawing.Point(104, 11); this.win2k.Name = "win2k"; this.win2k.Size = new System.Drawing.Size(80, 24); this.win2k.TabIndex = 1; this.win2k.Text = "支持win2k"; // // win9x // this.win9x.Location = new System.Drawing.Point(16, 11); this.win9x.Name = "win9x"; this.win9x.TabIndex = 0; this.win9x.Text = "支持win9x"; // // groupBox5 // this.groupBox5.Controls.Add(this.e_add); this.groupBox5.Controls.Add(this.e_modify); this.groupBox5.Location = new System.Drawing.Point(216, 176); this.groupBox5.Name = "groupBox5"; this.groupBox5.Size = new System.Drawing.Size(200, 40); this.groupBox5.TabIndex = 12; this.groupBox5.TabStop = false; // // e_add // this.e_add.Location = new System.Drawing.Point(112, 10); this.e_add.Name = "e_add"; this.e_add.Size = new System.Drawing.Size(80, 24); this.e_add.TabIndex = 5; this.e_add.Text = "允许添加"; this.e_add.CheckedChanged += new System.EventHandler(this.e_add_CheckedChanged); // // e_modify // this.e_modify.Location = new System.Drawing.Point(16, 10); this.e_modify.Name = "e_modify"; this.e_modify.Size = new System.Drawing.Size(76, 24); this.e_modify.TabIndex = 4; this.e_modify.Text = "允许修改"; this.e_modify.CheckedChanged += new System.EventHandler(this.e_modify_CheckedChanged); // // timer1 // this.timer1.Interval = 500; this.timer1.Tick += new System.EventHandler(this.timer1_Tick); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(424, 429); this.Controls.Add(this.groupBox5); this.Controls.Add(this.groupBox4); this.Controls.Add(this.button4); this.Controls.Add(this.b_add); this.Controls.Add(this.b_modify); this.Controls.Add(this.groupBox3); this.Controls.Add(this.groupBox1); this.Controls.Add(this.button1); this.Controls.Add(this.groupBox2); this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.MaximizeBox = false; this.Name = "Form1"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "API精灵FOR C#"; this.Load += new System.EventHandler(this.Form1_Load); this.groupBox1.ResumeLayout(false); this.groupBox2.ResumeLayout(false); this.groupBox3.ResumeLayout(false); this.groupBox4.ResumeLayout(false); this.groupBox5.ResumeLayout(false); this.ResumeLayout(false); } #endregion /// /// 应用程序的主入口点。 /// [STAThread] static void Main() { Application.Run(new Form1()); } [DllImport("user32.dll", EntryPoint="ShowWindow")] public static extern int ShowWindow ( int hwnd, int nCmdShow ); private void button1_Click(object sender, System.EventArgs e) { AboutForm myabout = new AboutForm(); myabout.ShowDialog(); } /// /// 填写mysearch的内容。 /// private void search_comp() { if (tiplist.SelectedIndex>-1) mysearch.Text = tiplist.SelectedItem.ToString(); tiplist.Visible = false ; mysearch.Select(); } /// /// 自动填写提示内容。 /// private void autocomp() { if (tiplist.SelectedIndex>-1) try { if (this.nowtypeselect==0) //================查询函数 { this.nowselect = tiplist.SelectedIndex; dlltext.Text = fundll[tiplist.SelectedIndex].ToString();//else tipsmemo.Text = funtips[tiplist.SelectedIndex].ToString(); codememo.Text = funcode[tiplist.SelectedIndex].ToString(); mat_text.Text = funmat[tiplist.SelectedIndex].ToString(); this.oldscoll_text = mat_text.Text; if (funwin9x[tiplist.SelectedIndex].ToString()==("Yes")) win9x.Checked=true; else win9x.Checked=false; if (funwin2k[tiplist.SelectedIndex].ToString()==("Yes")) win2k.Checked=true; else win2k.Checked=false; //滚动文字 if (mat_text.TextLength>30) timer1.Enabled = true; else timer1.Enabled = false; } else { dlltext.Text = ""; tipsmemo.Text = ""; codememo.Text = ""; win9x.Checked = false; win2k.Checked = false; } //******************** if (this.nowtypeselect==1) { this.nowselect = tiplist.SelectedIndex; codememo.Text = funcode[tiplist.SelectedIndex].ToString(); } //******************** //================= } catch { dlltext.Text = "没有找到相应连接库"; tipsmemo.Text = "没有找到相应提示"; codememo.Text = "没有找到相应调用代码"; mat_text.Text = "没有找到相应C#函数"; } } // /// /// 手动释放一些内存。 /// private void mydisp() { tipsmemo.Clear(); codememo.Clear(); tiplist.Items.Clear(); // ===== fundll.Clear();//保存读取出来的DLL内容 funtips.Clear();//保存读取出来的文提示信息 funcode.Clear();//保存读取出来的C#调用代码 funmat.Clear();//保存读取出来的C#对应函数 funwin9x.Clear();//保存读取出来的WIN9X funwin2k.Clear();//保存读取出来的WIN2K } private void mysearch_TextChanged(object sender, System.EventArgs e) { tiplist.Visible = true ; //自动完成功能。 tiplist.SelectedIndex = (tiplist.FindString(mysearch.Text,-1)) ;//加上这句,保证TIPLIST跟着自动变化 nowselect = tiplist.SelectedIndex; autocomp(); //设置提示信息 mytips(); } private void mysearch_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { if ((e.KeyCode == Keys.Down) || (e.KeyCode == Keys.Up)) tiplist.Focus(); if (e.KeyCode == Keys.Enter) { search_comp(); if (this.nowtypeselect==0) mykeyword(); if (this.nowtypeselect==1) mykeyword1(); } if (e.KeyCode == Keys.Escape) { tiplist.Visible = false ; } } private void tiplist_MouseLeave(object sender, System.EventArgs e) { } private void tiplist_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { } private void tiplist_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { this.search_comp(); if (this.nowtypeselect==0) mykeyword(); else mykeyword1(); } if (e.KeyCode == Keys.Escape) { tiplist.Visible = false ; } } private void tiplist_DoubleClick(object sender, System.EventArgs e) { this.search_comp(); if (this.nowtypeselect==0) mykeyword(); else mykeyword1(); } private void Form1_Load(object sender, System.EventArgs e) { //初始化数据库 initdatabase(); //MessageBox.Show(this,"欢迎使用 共享版\n 本版对使用功能上略有限制\n 且不提供数据库更新!\n 如想获得更多信息请与我联系。\n [email protected]","提示",MessageBoxButtons.OK,MessageBoxIcon.Information); select_type.SelectedIndex = 0; AboutForm myabout = new AboutForm(); myabout.ShowDialog(); } // private void mytips() { //设置提示信息 if ((this.nowselect>-1)&(this.nowtypeselect==0)) { toolTip1.SetToolTip(this.dlltext,"所在动态连接库: "+this.fundll[this.nowselect].ToString()); toolTip1.SetToolTip(this.mat_text,"在C#对应的函数: "+this.funmat[this.nowselect].ToString()); toolTip1.SetToolTip(this.codememo,"函数 "+this.tiplist.SelectedItem.ToString()+" 在C#的调用代码,可以手动修改"); toolTip1.SetToolTip(this.tipsmemo,"函数 "+this.tiplist.SelectedItem.ToString()+" 的注释信息,可以手动修改"); } else if ((this.nowselect>-1)&(this.nowtypeselect==1)) { toolTip1.SetToolTip(this.codememo,"常量 "+this.tiplist.SelectedItem.ToString()+" 在C#的调用代码"); toolTip1.SetToolTip(this.dlltext,"没有相关信息"); toolTip1.SetToolTip(this.mat_text,"没有相关信息"); toolTip1.SetToolTip(this.tipsmemo,"没有相关信息"); } } // private void initdatabase() { string dbpath = @nowpath+"winapi.mdb"; tiplist.Items.Clear(); //@"Provider=Microsoft.Jet.OleDB.4.0;Data Source="+dbpath+";User Id=admin;Password="+this.dbpassword ; //"Provider=Microsoft.Jet.OleDB.4.0;Data Source=your mdb filename;Jet OLEDB:Database Password='pass'" ; this.myConnstr = @"Provider=Microsoft.Jet.OleDB.4.0;Data Source="+dbpath+";User Id=admin;Jet OLEDB:Database Password="+this.dbpassword ; this.myconn= new OleDbConnection(myConnstr); mysql= @"select Fun_name,Fun_dll,Fun_tips,Fun_code,Fun_com,win9x,win2k from winapi"; using(myconn) { myconn.Open(); // if (myconn.State.ToString() == "Open") MessageBox.Show("打开成功!"); //数据处理 // OleDbCommand mycommand = new OleDbCommand(mysql,myconn); mycommand.CommandText = mysql; mycommand.Connection = myconn; OleDbDataReader myreader = mycommand.ExecuteReader(); int i=0; while (myreader.Read()) { tiplist.Items.Add(myreader["Fun_name"]); fundll.Add(myreader["fun_dll"].ToString()); funtips.Add(myreader["fun_tips"].ToString()); funcode.Add(myreader["fun_code"].ToString()); funmat.Add(myreader["fun_com"].ToString()); funwin9x.Add(myreader["win9x"].ToString()); funwin2k.Add(myreader["win2k"].ToString()); i++; } myconn.Close(); myreader.Close(); } } //更新缓存 private void memo_update() { fundll[nowselect] = dlltext.Text; funtips[nowselect] = this.tipsmemo.Text; funcode[nowselect] = this.codememo.Text; funmat[nowselect] = this.mat_text.Text; if (win9x.Checked) funwin9x[nowselect]="Yes" ;else funwin9x[nowselect]="No"; if (win2k.Checked) funwin2k[nowselect]="Yes" ;else funwin2k[nowselect]="No"; } // private void oleDbConnection1_InfoMessage(object sender, System.Data.OleDb.OleDbInfoMessageEventArgs e) { } //==============从WINAPI.TXT读取CONST并拆分 private void mysplip() { //string dbpath = @System.Environment.CurrentDirectory+@"\winapi.mdb"; string filename =@nowpath +"winapi.txt"; string nextline; tiplist.Items.Clear(); StreamReader sr = new StreamReader(filename); while ((nextline = sr.ReadLine())!=null) { if (nextline.StartsWith("public const")) { string[] ss = nextline.Split('='); tiplist.Items.Add(ss[0].Substring(16).Trim()); funcode.Add(nextline); } } sr.Close(); } //============================== private void select_type_SelectedIndexChanged(object sender, System.EventArgs e) { if (select_type.SelectedIndex != this.nowtypeselect) { this.mydisp(); switch (select_type.SelectedIndex) { case 0:initdatabase();this.nowtypeselect=select_type.SelectedIndex;break; case 1:mysplip();this.nowtypeselect=select_type.SelectedIndex;this.dlltext.Clear();this.mat_text.Clear();break; } isenable(this.nowtypeselect); } } private void dlltext_TextChanged(object sender, System.EventArgs e) { } private void richTextBox1_MouseEnter(object sender, System.EventArgs e) { tiplist.Visible = false ; } private void mysearch_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { tiplist.Visible = false ; mysearch.Focus(); } private void tiplist_SelectedIndexChanged(object sender, System.EventArgs e) { autocomp(); } /// /// 修改内容。 /// private void fun_update() { using(myconn) { myconn.ConnectionString = myConnstr; myconn.Open(); try { //if (myconn.State.ToString() == "Open") MessageBox.Show("打开成功!"); isnull();//判断是否有无效值 string str_win9x,str_win2k; if (win9x.Checked) str_win9x = "Yes" ; else str_win9x = "No"; if (win2k.Checked) str_win2k = "Yes" ; else str_win2k = "No"; sql_update = "update winapi set Fun_dll = '"+dlltext.Text+"'"+" , Fun_tips = '"+tipsmemo.Text+"'"+" , Fun_code = '"+codememo.Text+"'"+" , Fun_com ='"+mat_text.Text+"' "; sql_update +=" , win9x = '" + str_win9x +"' " + ", win2k = '" + str_win2k+"' "; sql_update +=" where Fun_name ='"+ mysearch.Text+"'"; mycommand.Connection = myconn; mycommand.CommandText = sql_update; mycommand.ExecuteNonQuery(); myconn.Close(); memo_update(); MessageBox.Show(this,"恭喜!更新成功!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information); } catch { // tipsmemo.Text = sql_update; MessageBox.Show("没有找到相应记录,更新失败!"); } } } //判断是更新的部分是否有效(不能为空) private void isnull() { if (this.mat_text.Text=="") this.mat_text.Text="没有相关信息"; if (this.codememo.Text=="") this.codememo.Text="没有相关信息"; if (this.tipsmemo.Text=="") this.tipsmemo.Text="没有相关信息"; if (this.dlltext.Text=="") this.dlltext.Text="没有相关信息"; } //关键字高亮显示 private void mykeyword() { string[] keywords = new string[5]; keywords[0]=mysearch.Text; keywords[1]="string"; keywords[2]="ref"; keywords[3]="int"; keywords[4]="static extern"; for(int i=0;i0) { index++; switch(i) { case 0: codememo.SelectionColor = Color.Red;break; case 1: codememo.SelectionColor = Color.Green;break; case 2: codememo.SelectionColor = Color.Brown;break; case 3: codememo.SelectionColor = Color.Blue;break; case 4: codememo.SelectionColor = Color.Green;break; //default:codememo.SelectionColor = Color.Blue;break; } } } } // //CONST关键字高亮显示 private void mykeyword1() { string[] keywords = new string[5]; keywords[0]=mysearch.Text; keywords[1]="="; keywords[2]="0"; keywords[3]="int"; keywords[4]="const"; for(int i=0;i0) { index++; switch(i) { case 0: codememo.SelectionColor = Color.Red;break; case 1: codememo.SelectionColor = Color.Blue;break; case 2: codememo.SelectionColor = Color.Green;break; case 3: codememo.SelectionColor = Color.Blue;break; case 4: codememo.SelectionColor = Color.Green;break; //default:codememo.SelectionColor = Color.Blue;break; } if (index>codememo.TextLength) break; } } } // private void button2_Click(object sender, System.EventArgs e) { this.fun_update(); } private void isenable(int i_temp) { if (i_temp==0) { win9x.Enabled=true; win2k.Enabled=true; e_modify.Enabled=true; e_add.Enabled=true; b_modify.Enabled=true; b_add.Enabled=true; } else { win9x.Enabled=false; win2k.Enabled=false; e_modify.Enabled=false; e_add.Enabled=false; b_modify.Enabled=false; b_add.Enabled=false; } } private void button4_Click(object sender, System.EventArgs e) { Application.Exit(); } private void e_modify_CheckedChanged(object sender, System.EventArgs e) { if (e_modify.Checked) b_modify.Enabled = true; else b_modify.Enabled = false; } private void e_add_CheckedChanged(object sender, System.EventArgs e) { if (e_add.Checked) b_add.Enabled = true; else b_add.Enabled = false; } private void timer1_Tick(object sender, System.EventArgs e) { if (this.mat_text.TextLength>0) mat_text.Text = mat_text.Text.Remove(0,1); else mat_text.Text = oldscoll_text; } private void mat_text_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { timer1.Enabled = false ; mat_text.Text = oldscoll_text; } private void codememo_TextChanged(object sender, System.EventArgs e) { } private void tiplist_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) { } private void menuItem1_Click(object sender, System.EventArgs e) { Control ct = this.ActiveControl; string temp = ct.Text; Clipboard.SetDataObject(temp); } private void menuItem2_Click(object sender, System.EventArgs e) { } } }
1、SqlConnection类   构造函数:SqlConnection(connstr);   属性:  Database//获取当前数据库或连接打开后要使用的数据库的名称        Connectionstring//获取或设置用于打开 SQL Server 数据库的字符串   方法:  Open()        Close()        Dispose()//释放所有资源         2、SqlCommand类     构造函数:SqlCommand(string,conn) 属性:  CommandType//获取或设置一个值,该值指示如何解释 CommandText 属         CommandText //获取或设置要对数据源执行SQL 语句         Connection//获取或设置 SqlCommand 的此实例使用的 SqlConnection         Parameters//获取 SqlParameterCollection。参数集合 方法:  ExecuteNonQuery(): 返回受影响函数,如增、删、改操作;         ExecuteScalar():执行查询,返回首行首列的结果;         ExecuteReader():返回一个数据流(SqlDataReader对象)。   实例1: SqlCommand cmd = new SqlCommand () cmd.connection =conn; cmd.CommandType = CommandType.Text; cmd.CommandText ="select *from produce=@ID"; cmd.Parameters.Add("@ID",SqlDBType.NVarChar,10,ID).values=1; cmd.ExecuteScalar();   实例2:   SqlCommand cmd = new SqlCommand (“select *from test”,conn);   cmd.ExecuteScalar(); 注意: ExecuteNonQuery()方法主要用户更新数据,通常它使用Update,Insert,Delete语句来操作数据库,其方法返回值意义:对于 Update,Insert,Delete 语句 执行成功是返回值为该命令所影响的行数,如果影响的行数为0时返回的值为0,如果数据操作回滚得话返回值为-1,对于这种更新操作 用我们平时所用的是否大于0的判断操作应该没有问题而且比较好,但是对于其他的操作如对数据库结构的操作,如果操作成功时返回的却是-1,这种情况跟我们平时的思维方式有点差距所以应该好好的注意了, 3、SqlDataReader类   是由ExecuteReader()返回一个数据流(SqlDataReader对象)没有构造方法   属性:Connection//获取与 SqlDataReader 关联的 SqlConnection。      FieldCount//获取当前行的列数。      HasRows//获取一个值,该值指示 SqlDataReader 是否包含一行或多行      RecordsAffect//获取执行 Transact-SQL 语句所更改、插入或删除的行数   方法:Read();//使 SqlDataReader 前进到下一条记录      GetType();//获取当前实例的 Type      NextResult();//当读取批处理 Transact-SQL 语句的结果时,使数据读取器前进到下一个结果 4、SqlDataApater类   构造方法:SqlDataApater()        SqlDataAdapter(SqlCommand)//初始化 SqlDataAdapter 类的新实例,用指定的 SqlCommand 作为 SelectCommand 的属性。        SqlDataApater(string,conn)//使用 SelectCommand 和 SqlConnection 对象初始化 SqlDataAdapter 类的一个新实例   属性:  DeleteCommand        SelectCommand        InsertCommand        UpdataCommand              方法:  Fill(DataSet)//在 DataSet 添加或刷新行        Fill(DataTable)//在 DataSet 的指定范围添加或刷新行,以与使用 DataTable 名称的数据源的行匹配。 (继承自DbDataAdapter。        Update(DataRow[])//通过为 DataSet 的指定数组的每个已插入、已更新或已删除的行执行相应的 INSERT、UPDATE 或 DELETE 语句来更新数据库的值        Update(DataSet)//通过为指定的 DataTable 的每个已插入、已更新或已删除的行执行相应的 INSERT、UPDATE 或 DELETE 语句来更新数据库的值        Update(DataTable) 实例1   // 隐藏了SqlCommand对象的定义,同时隐藏了SqlCommand对象与SqlDataAdapter对象的绑定 SqlDataAdapter myda= new SqlDataAdapter("select * from test",conn);   实例2   SqlCommand mySqlCommand = new SqlCommand();// 创建SqlCommand   mySqlCommand.CommandType = CommandType.Text;   mySqlCommand.CommandText = "select * from product";   mySqlCommand.Connection = sqlCnt;   SqlDataAdapter myDataAdapter = new SqlDataAdapter();   // 创建SqlDataAdapter   myDataAdapter.SelectCommand = mySqlCommand; // 为SqlDataAdapter对象绑定所要执行的SqlCommand对象 5、DataSet类     命名空间:System.Data.DataSet。   数据集,本地微型数据库,可以存储多张表。  //使用DataSet第一步就是将SqlDataAdapter返回的数据集(表)填充到Dataset对象:   SqlDataAdapter myDataAdapter = new SqlDataAdapter("select * from product", sqlCnt); DataSet myDataSet = new DataSet(); // 创建DataSet myDataAdapter.Fill(myDataSet, "product"); // 将返回的数据集作为“表”填入DataSet,表名可以与数据库真实的表名不同,并不影响后续的增、删、改等操作 //访问dataset的数据 DataTable myTable = myDataSet.Tables["product"]; foreach (DataRow myRow in myTable.Rows) { foreach (DataColumn myColumn in myTable.Columns) { Console.WriteLine(myRow[myColumn]); //遍历表的每个单元格 } } // 修改DataSet DataTable myTable = myDataSet.Tables["product"]; foreach (DataRow myRow in myTable.Rows) { myRow["name"] = myRow["name"] + "商品"; } // 将DataSet的修改提交至“数据库” SqlCommandBuilder mySqlCommandBuilder = new SqlCommandBuilder(myDataAdapter); myDataAdapter.Update(myDataSet, "product"); // 添加一行 DataRow myRow = myTable.NewRow(); myRow["name"] = "捷安特"; myRow["price"] = 13.2; //myRow["id"] = 100; id若为“自动增长”,此处可以不设置,即便设置也无效 myTable.Rows.Add(myRow); // 将DataSet的修改提交至“数据库” SqlCommandBuilder mySqlCommandBuilder = new SqlCommandBuilder(myDataAdapter); myDataAdapter.Update(myDataSet, "product"); // 删除第一行 DataTable myTable = myDataSet.Tables["product"]; myTable.Rows[0].Delete(); SqlCommandBuilder mySqlCommandBuilder = new SqlCommandBuilder(myDataAdapter); myDataAdapter.Update(myDataSet, "product");
以下是使用 C#MySqlDataAdapter 实现异步用户登录并将数据写入 DataSet 的基本步骤: 1. 首先,您需要创建一个连接到 MySQL 数据库MySqlConnection 对象。您需要提供数据库的连接字符串,该字符串包括数据库的名称、服务器名称、用户名和密码等信息。例如: ``` string connectionString = "server=localhost;database=myDatabase;uid=myUsername;pwd=myPassword;"; MySqlConnection connection = new MySqlConnection(connectionString); ``` 2. 接下来,创建一个 MySqlDataAdapter 对象并为其设置 SELECT 命令。例如: ``` MySqlDataAdapter adapter = new MySqlDataAdapter("SELECT * FROM users WHERE username=@username AND password=@password", connection); adapter.SelectCommand.Parameters.AddWithValue("@username", username); adapter.SelectCommand.Parameters.AddWithValue("@password", password); ``` 这里,我们使用了一个参数化的 SELECT 命令,其 @username 和 @password 是参数,它们将由用户输入的用户名和密码值替换。 3. 创建一个 DataSet 对象,并使用 MySqlDataAdapter 的 FillAsync 方法将数据异步填充到 DataSet 。例如: ``` DataSet dataSet = new DataSet(); await adapter.FillAsync(dataSet); ``` 4. 检查 DataSet 是否有用户信息。例如: ``` if (dataSet.Tables[0].Rows.Count > 0) { // 用户登录成功,可以执行其他操作 } else { // 用户名或密码不正确 } ``` 完整的代码示例: ``` string connectionString = "server=localhost;database=myDatabase;uid=myUsername;pwd=myPassword;"; MySqlConnection connection = new MySqlConnection(connectionString); MySqlDataAdapter adapter = new MySqlDataAdapter("SELECT * FROM users WHERE username=@username AND password=@password", connection); adapter.SelectCommand.Parameters.AddWithValue("@username", username); adapter.SelectCommand.Parameters.AddWithValue("@password", password); DataSet dataSet = new DataSet(); await adapter.FillAsync(dataSet); if (dataSet.Tables[0].Rows.Count > 0) { // 用户登录成功,可以执行其他操作 } else { // 用户名或密码不正确 } ``` 请注意,这只是一个基本的示例,您需要根据自己的需求进行修改和扩展。同时,请确保您的代码安全可靠,防范 SQL 注入等攻击。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值