我有一個小問題。當我創建VS2012的腳本,它看起來像 (數據庫名稱:LSProjeDB,有一個表作爲Musteri)(如創建:去表>更新> genarete SCRIPT>哇)在c#中用Visual Studio 2012創建.sql腳本以及如何運行它?
GO SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, CONCAT_NULL_YIELDS_NULL, QUOTED_IDENTIFIER ON;
SET NUMERIC_ROUNDABORT OFF;
GO
:setvar DatabaseName "C:\USERS\YC\DOCUMENTS\LSPROJEDB.MDF"
:setvar DefaultFilePrefix "C_\USERS\YC\DOCUMENTS\LSPROJEDB.MDF_"
:setvar DefaultDataPath "C:\Users\YC\AppData\Local\Microsoft\Microsoft SQL Server Local DB\Instances\v11.0\"
:setvar DefaultLogPath "C:\Users\YC\AppData\Local\Microsoft\Microsoft SQL Server Local DB\Instances\v11.0\"
GO
:on error exit
GO
/*
Detect SQLCMD mode and disable script execution if SQLCMD mode is not supported.
To re-enable the script after enabling SQLCMD mode, execute the following:
SET NOEXEC OFF;
*/
:setvar __IsSqlCmdEnabled "True"
GO
IF N'$(__IsSqlCmdEnabled)' NOT LIKE N'True'
BEGIN
PRINT N'SQLCMD mode must be enabled to successfully execute this script.';
SET NOEXEC ON;
END
GO
USE [$(DatabaseName)];
GO
PRINT N'Update complete.'
GO
但看起來不對! 和我使用的C#代碼:
string str = "DataSource=(local);Initial Catalog=DatabaseName ;Integrated Security=True";
FileInfo file = new FileInfo("D:\\SQLQuery2.sql");
string script = file.OpenText().ReadToEnd();
SqlConnection conn = new SqlConnection(str);
Server server = new Server(new ServerConnection(conn));
server.ConnectionContext.ExecuteNonQuery(script);
和我得到:
ArgumentException的是未處理! 不支持的關鍵字:'datasource'。
請使用bastardised英語避免。 「你」和「4」不是單詞。 –
問題在於連接字符串的格式。我沒有正確的語法,但這可能很有用:http://connectionstrings.com/ – JcFx
@ J.Steen對不起。 – Acablack