我在一个文件中有一个mysql脚本,我需要能够从我的c#应用程序执行。以下是脚本包含的示例:
USE osae;
-- Set DB version
CALL osae_sp_object_property_set('SYSTEM', 'DB Version', '0.3.5', '', '');
CALL osae_sp_object_property_set('SYSTEM', 'Debug', 'FALSE', '', '');
CALL osae_sp_object_type_property_add ('Prune Logs','Boolean','TRUE','SYSTEM',0);
CALL osae_sp_object_property_set ('SYSTEM','Prune Logs','TRUE','','');
DELIMITER $$
DROP PROCEDURE IF EXISTS osae_sp_object_event_script_update$$
CREATE DEFINER = 'root'@'localhost'
PROCEDURE osae_sp_object_event_script_update(IN pobject varchar(200), IN pevent varchar(200), IN ptext text)
BEGIN
DECLARE vObjectCount INT;
DECLARE vObjectID INT;
DECLARE vObjectTypeID INT;
DECLARE vEventCount INT;
DECLARE vEventID INT;
SELECT COUNT(object_id) INTO vObjectCount FROM osae_object WHERE UPPER(object_name)=UPPER(pobject);
IF vObjectCount > 0 THEN
SELECT object_id,object_type_id INTO vObjectID,vObjectTypeID FROM osae_object WHERE UPPER(object_name)=UPPER(pobject);
SELECT COUNT(event_id) INTO vEventCount FROM osae_object_type_event WHERE object_type_id=vObjectTypeID AND (UPPER(event_name)=UPPER(pevent) OR UPPER(event_label)=UPPER(pevent));
IF vEventCount = 1 THEN
SELECT event_id INTO vEventID FROM osae_object_type_event WHERE object_type_id=vObjectTypeID AND (UPPER(event_name)=UPPER(pevent) OR UPPER(event_label)=UPPER(pevent));
UPDATE osae_object_event_script SET event_script=ptext WHERE object_id=vObjectID AND event_id=vEventID;
-- CALL osae_sp_debug_log_add(CONCAT('Updated ',vObjectID,' - ',vEventID,ptext),'');
END IF;
END IF;
END
$$
DELIMITER ;
正如您所看到的那样,它有多种行混合调用存储过程,还有一些drop和create语句来更新其他存储过程。
我尝试过两种不同的方法来执行脚本,但都失败了:
MySqlScript script = new MySqlScript(connection, File.ReadAllText("script.sql"));
script.Execute();
抛出异常:索引超出了数组的范围。
MySqlCommand upgCommand = new MySqlCommand();
upgCommand.Connection = connection;
upgCommand.CommandText = File.ReadAllText("script.sql");
upgCommand.ExecuteNonQuery();
这引发了一个异常,即我的sql语法中存在错误。
当我在dbForge Studio中手动运行整个脚本时,它会完美执行。如何从我的C#app
中正确执行此脚本答案 0 :(得分:8)
看看here。您应该为MySqlScript指定一个分隔符(因为那里有存储过程)。您的查询应如下所示:
-- Set DB version
CALL osae_sp_object_property_set('SYSTEM', 'DB Version', '0.3.5', '', '')$$
CALL osae_sp_object_property_set('SYSTEM', 'Debug', 'FALSE', '', '')$$
CALL osae_sp_object_type_property_add ('Prune Logs','Boolean','TRUE','SYSTEM',0)$$
CALL osae_sp_object_property_set ('SYSTEM','Prune Logs','TRUE','','')$$
DROP PROCEDURE IF EXISTS osae_sp_object_event_script_update$$
CREATE DEFINER = 'root'@'localhost'
PROCEDURE osae_sp_object_event_script_update(IN pobject varchar(200), IN pevent varchar(200), IN ptext text)
BEGIN
DECLARE vObjectCount INT;
DECLARE vObjectID INT;
DECLARE vObjectTypeID INT;
DECLARE vEventCount INT;
DECLARE vEventID INT;
SELECT COUNT(object_id) INTO vObjectCount FROM osae_object WHERE UPPER(object_name)=UPPER(pobject);
IF vObjectCount > 0 THEN
SELECT object_id,object_type_id INTO vObjectID,vObjectTypeID FROM osae_object WHERE UPPER(object_name)=UPPER(pobject);
SELECT COUNT(event_id) INTO vEventCount FROM osae_object_type_event WHERE object_type_id=vObjectTypeID AND (UPPER(event_name)=UPPER(pevent) OR UPPER(event_label)=UPPER(pevent));
IF vEventCount = 1 THEN
SELECT event_id INTO vEventID FROM osae_object_type_event WHERE object_type_id=vObjectTypeID AND (UPPER(event_name)=UPPER(pevent) OR UPPER(event_label)=UPPER(pevent));
UPDATE osae_object_event_script SET event_script=ptext WHERE object_id=vObjectID AND event_id=vEventID;
-- CALL osae_sp_debug_log_add(CONCAT('Updated ',vObjectID,' - ',vEventID,ptext),'');
END IF;
END IF;
END
$$
然后你的代码:
MySqlScript script = new MySqlScript(connection, File.ReadAllText("script.sql"));
script.Delimiter = "$$";
script.Execute();
答案 1 :(得分:1)
您可以运行单独的Process来执行脚本。例如:
Process.Start("mysql < script.sql");
根据您的环境,您可能需要稍微使用它来包含mysql可执行文件或sql脚本的路径。
答案 2 :(得分:1)
如果将sql脚本放在存储过程中怎么办?如果您仍然希望使用这种方式将sql文件作为文件附加到应用程序资源(文件.resx),并执行它:
MySql.Data.MySqlClient.MySqlCommand cmdMySQL = newMySqlConnection.CreateCommand();
cmdMySQL.CommandText = (System.String)globalResource.your_file_name;
答案 3 :(得分:0)
我不是100%确定原生MySql语法是什么,但是当我们为Sql Server执行类似的功能时,我们必须根据仅使用的文字值(GO
)将sql文件分成块由查询执行程序(Sql Server管理控制台)。
我怀疑上面的请求中可能嵌入了类似的信息,例如DEFINE语句。
确切知道MySql所抱怨的内容将有助于改进解决方案。