C#连接mysql数据不能存档,原因是没有:SQLCommandBuilder功能吗?

qq_32090585 2016-11-06 05:12:38
为什么MySQLDriverCS.dll没有象mssql一样有:SQLCommandBuilder

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data;
using MySQLDriverCS;

........

//--------------------------------------------------------------------------------------------
private void btnSave_Click(object sender, EventArgs e)
{

try
{
Common.SQLtran = (MySQLTransaction)Common.SQLConn.BeginTransaction();
objdt_Command.Transaction = Common.SQLtran;
this.BindingContext[objdt_City].Position = this.BindingContext[objdt_City].Count - 1;
this.BindingContext[objdt_City].Position = 0;
objdt_Adater.Update(objdt_City);
objdt_City.AcceptChanges();
Common.SQLtran.Commit();
MessageBox.Show("存檔完成", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
Common.SQLtran.Rollback();
objdt_DataSet.RejectChanges();
MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

//Initialize
objdt_City.Clear();
//**************
FuncRetrieve();
//**************

}
...全文
134 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
LongRui888 2016-11-07
  • 打赏
  • 举报
回复
如果是真的没有,那就自己构建一个update语句,修改数据吧
ACMAIN_CHM 2016-11-06
  • 打赏
  • 举报
回复
引用
20.2.4.1.2. The MySqlCommand Object Once a connection has been established with the MySQL database, the next step is do carry out the desired database operations. This can be achieved through the use of the MySqlCommand object. You will see how to create a MySqlCommand object. Once it has been created there are three main methods of interest that you can call: ExecuteReader - used to query the database. Results are usually returned in a MySqlDataReader object, created by ExecuteReader. ExecuteNonQuery - used to insert and delete data. ExecuteScalar - used to return a single value. Once a MySqlCommand object has been created, you will call one of the above methods on it to carry out a database operation, such as perform a query. The results are usually returned into a MySqlDataReader object, and then processed, for example the results might be displayed. The following code demonstrates how this could be done. using System; using System.Data; using MySql.Data; using MySql.Data.MySqlClient; public class Tutorial2 { public static void Main() { string connStr = "server=localhost;user=root;database=world;port=3306;password=******;"; MySqlConnection conn = new MySqlConnection(connStr); try { Console.WriteLine("Connecting to MySQL..."); conn.Open(); string sql = "SELECT Name, HeadOfState FROM Country WHERE Continent='Oceania'"; MySqlCommand cmd = new MySqlCommand(sql, conn); MySqlDataReader rdr = cmd.ExecuteReader(); while (rdr.Read()) { Console.WriteLine(rdr[0]+" -- "+rdr[1]); } rdr.Close(); conn.Close(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } Console.WriteLine("Done."); } } When a connection has been created and opened, the code then creates a MySqlCommand object. Note that the SQL query to be executed is passed to the MySqlCommand constructor. The ExecuteReader method is then used to generate a MySqlReader object. The MySqlReader object contains the results generated by the SQL executed on the command object. Once the results have been obtained in a MySqlReader object, the results can be processed. In this case the information is simply printed out as part of a while loop. Finally, the MySqlReader object is displosed of by running its Close method on it. In the next example you will see how to use the ExecuteNonQuery method. The procedure for performing an ExecuteNonQuery method call is simpler, as there is no need to create an object to store results. This is because ExecuteNonQuery is only used for inserting, updating and deleting data. The following example illustrates a simple update to the Country table: using System; using System.Data; using MySql.Data; using MySql.Data.MySqlClient; public class Tutorial3 { public static void Main() { string connStr = "server=localhost;user=root;database=world;port=3306;password=******;"; MySqlConnection conn = new MySqlConnection(connStr); try { Console.WriteLine("Connecting to MySQL..."); conn.Open(); string sql = "INSERT INTO Country (Name, HeadOfState, Continent) VALUES ('Disneyland','Mickey Mouse', 'North America')"; MySqlCommand cmd = new MySqlCommand(sql, conn); cmd.ExecuteNonQuery(); conn.Close(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } Console.WriteLine("Done."); } } The query is constructed, the command object created and the ExecuteNonQuery method called on the command object. You can access your MySQL database with the MySQL Client program and verify that the update was carried out correctly. Finally, you will see how the ExecuteScalar method can be used to return a single value. Again, this is straightforward, as a MySqlDataReader object is not required to store results, a simple variable will do. The following code illustrates how to use ExecuteScalar: using System; using System.Data; using MySql.Data; using MySql.Data.MySqlClient; public class Tutorial4 { public static void Main() { string connStr = "server=localhost;user=root;database=world;port=3306;password=******;"; MySqlConnection conn = new MySqlConnection(connStr); try { Console.WriteLine("Connecting to MySQL..."); conn.Open(); string sql = "SELECT COUNT(*) FROM Country"; MySqlCommand cmd = new MySqlCommand(sql, conn); object result = cmd.ExecuteScalar(); if (result != null) { int r = Convert.ToInt32(result); Console.WriteLine("Number of countries in the World database is: " + r); } conn.Close(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } Console.WriteLine("Done."); } } This example uses a simple query to count the rows in the Country table. The result is obtained by calling ExecuteScaler on the command object.
.
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");

56,687

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧