如何设置两个连接字符串 MySqlCommand 对象参数?
Posted
技术标签:
【中文标题】如何设置两个连接字符串 MySqlCommand 对象参数?【英文标题】:how to set two connection string MySqlCommand object argument? 【发布时间】:2020-05-14 19:20:36 【问题描述】:我想创建基于迁移的 Visual Basic 应用程序,用于将 mysql 数据库表迁移到 MS SQL Server 2014,以便通过一键单击事件轻松完成,它将开始迁移您要迁移的任何数据库的数据
这是我编写的主要代码,但是当我想在 MySqlCommand 参数中连接两个连接时,这怎么可能
private void Start_Click(object sender, EventArgs e)
//MS SQL Connection server
string connectionString;
SqlConnection MSSQL;
connectionString = @"Data Source=DESKTOP-IF12M9R;Initial Catalog=utility;User ID=sa;Password=mysql";
MSSQL = new SqlConnection(connectionString);
MSSQL.Open();
MessageBox.Show("Connection Open of MS SQL !");
//MySQL server Connectivity
string connStr = "server=localhost;user=root;database=utility;port=3306;password=''";
MySqlConnection conn = new MySqlConnection(connStr);
try
MessageBox.Show("Connecting to MySQL...");
conn.Open();
MessageBox.Show("Connection open of MySQL!");
string sql = "SELECT * FROM userial";
MessageBox.Show(sql);
MySqlCommand cmd = new MySqlCommand(sql, conn);
MySqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
MessageBox.Show(rdr[0] + " -- " + rdr[1] + " -- " + rdr[2] + " -- " + rdr[3]);
rdr.Close();
string mytoms = "IF EXISTS (SELECT * FROM sys.objects so JOIN sys.schemas sc ON so.schema_id = sc.schema_id WHERE so.name = N'userial' AND sc.name = N'utility' AND type in (N'U')) BEGIN DECLARE @drop_statement nvarchar(500) DECLARE drop_cursor CURSOR FOR SELECT 'alter table ' + quotename(schema_name(ob.schema_id)) + '.' + quotename(object_name(ob.object_id)) + ' drop constraint ' + quotename(fk.name) FROM sys.objects ob INNER JOIN sys.foreign_keys fk ON fk.parent_object_id = ob.object_id WHERE fk.referenced_object_id = ( SELECT so.object_id FROM sys.objects so JOIN sys.schemas sc ON so.schema_id = sc.schema_id WHERE so.name = N'userial' AND sc.name = N'utility' AND type in (N'U')) OPEN drop_cursor FETCH NEXT FROM drop_cursor INTO @drop_statement WHILE @@FETCH_STATUS = 0 BEGIN EXEC(@drop_statement) FETCH NEXT FROM drop_cursor INTO @drop_statement END CLOSE drop_cursor DEALLOCATE drop_cursor DROP TABLE[utility].[userial] END GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [utility].[userial] ([SerialID] int NOT NULL, [SerialNo] varchar(50) NOT NULL, [DateTime] datetime2(0) NOT NULL, [Result] varchar(4) NOT NULL WITH(DATA_COMPRESSION = NONE) GO IF EXISTS(SELECT* FROM sys.objects so JOIN sys.schemas sc ON so.schema_id = sc.schema_id WHERE so.name = N'PK_userial_SerialID' AND sc.name = N'utility' AND type in (N'PK')) ALTER TABLE[utility].[userial] DROP CONSTRAINT[PK_userial_SerialID] GO ALTER TABLE[utility].[userial] ADD CONSTRAINT[PK_userial_SerialID] PRIMARY KEY CLUSTERED([SerialID] ASC) GO ALTER TABLE[utility].[userial] ADD DEFAULT getdate() FOR[DateTime] GO";
MessageBox.Show(mytoms);
MySqlCommand cmd1 = new MySqlCommand(mytoms , conn, MSSQL);
MySqlDataReader rdr1 = cmd1.ExecuteReader();
while (rdr1.Read())
MessageBox.Show(rdr1[0] + " -- " + rdr1[1] + " -- " + rdr1[2] + " -- " + rdr1[3]);
rdr.Close();
catch (Exception ex)
Console.WriteLine(ex.ToString());
现在主要问题在这里
MySqlCommand cmd1 = new MySqlCommand(mytoms , conn, MSSQL);
有什么解决办法吗?
【问题讨论】:
你应该声明一个专用于 sql server 的连接和另一个专用于 mysql 的连接 你不应该在MySqlCommand
中有两个连接(我认为这完全不可能)。只需在 while (rdr.Read())
循环中使用适当的 SQL 命令执行 cmd1.ExecuteNonQuery()
。
【参考方案1】:
在经历了一些失败之后,我终于得到了这个解决方案的成功
public partial class Form1 : Form
//MS SQL Connection server
public string connectionString;
public SqlConnection MSSQL;
//MySQL server Connectivity
public string connStr;
public MySqlConnection conn;
private void Start_Click(object sender, EventArgs e)
try
//Connection Process of MS SQL Server
connectionString = @"Data Source='your source name';Initial Catalog='database name';User ID='username';Password='password'";
MSSQL = new SqlConnection(connectionString);
//opening connection of MS SQL Server
MSSQL.Open();
MessageBox.Show("Connection Open of MS SQL !");
//Connection Process of MySQL Server
connStr = "server='server name';user='username';database='database name';port='port';password='password'";
conn = new MySqlConnection(connStr);
//opening connection of MySQL Server
conn.Open();
MessageBox.Show("Connection open of MySQL!");
catch (Exception ex)
Console.WriteLine(ex.ToString());
【讨论】:
以上是关于如何设置两个连接字符串 MySqlCommand 对象参数?的主要内容,如果未能解决你的问题,请参考以下文章
分享一个Mysql操作方法类(MySql数据库连接执行MySqlCommand命令创建MySqlDataReader对象)
C#:使用 CancellationToken 取消 MySqlCommand,给出 NULLReferenceException
使用 MySqlCommand 在一个查询中进行多个 INSERT
使用 MySqlCommand 执行存储过程时出现 SqlNullValueException