本文整理汇总了C#中ServerConnection.ExecuteNonQuery方法的典型用法代码示例。如果您正苦于以下问题:C# ServerConnection.ExecuteNonQuery方法的具体用法?C# ServerConnection.ExecuteNonQuery怎么用?C# ServerConnection.ExecuteNonQuery使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ServerConnection
的用法示例。
在下文中一共展示了ServerConnection.ExecuteNonQuery方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ExecuteSql
protected void ExecuteSql(string sql)
{
using (var connection = new SqlConnection(this.ConnectionString))
{
var serverConnection = new ServerConnection(connection);
serverConnection.ExecuteNonQuery(sql);
}
}
示例2: CreateDatabase
public static void CreateDatabase(Identifier claimedId, string friendlyId, string databaseName) {
const string SqlFormat = @"
{0}
GO
EXEC [dbo].[AddUser] 'admin', 'admin', '{1}', '{2}'
GO
";
var removeSnippets = new string[] { @"
IF IS_SRVROLEMEMBER(N'sysadmin') = 1
BEGIN
IF EXISTS (SELECT 1
FROM [master].[dbo].[sysdatabases]
WHERE [name] = N'$(DatabaseName)')
BEGIN
EXECUTE sp_executesql N'ALTER DATABASE [$(DatabaseName)]
SET HONOR_BROKER_PRIORITY OFF
WITH ROLLBACK IMMEDIATE';
END
END
ELSE
BEGIN
PRINT N'The database settings cannot be modified. You must be a SysAdmin to apply these settings.';
END
GO" };
string databasePath = HttpContext.Current.Server.MapPath("~/App_Data/" + databaseName + ".mdf");
StringBuilder schemaSqlBuilder = new StringBuilder();
using (var sr = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(DefaultNamespace + ".CreateDatabase.sql"))) {
schemaSqlBuilder.Append(sr.ReadToEnd());
}
foreach (string remove in removeSnippets) {
schemaSqlBuilder.Replace(remove, string.Empty);
}
schemaSqlBuilder.Replace("$(Path1)", HttpContext.Current.Server.MapPath("~/App_Data/"));
schemaSqlBuilder.Replace("WEBROOT", databasePath);
schemaSqlBuilder.Replace("$(DatabaseName)", databaseName);
string sql = string.Format(CultureInfo.InvariantCulture, SqlFormat, schemaSqlBuilder, claimedId, "Admin");
var serverConnection = new ServerConnection(".\\sqlexpress");
try {
serverConnection.ExecuteNonQuery(sql);
} finally {
try {
var server = new Server(serverConnection);
server.DetachDatabase(databaseName, true);
} catch (FailedOperationException) {
}
serverConnection.Disconnect();
}
}
示例3: InvokeHandler
public override void InvokeHandler(CommandSetState state)
{
var diagram = state.CurrentDocView.CurrentDiagram;
var modelRoot = diagram.Store.ElementDirectory.FindElements<ModelRoot>().Single();
var connectionString = modelRoot.ConnectionString;
var dlg = new GenerateSQLForm();
if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
var sqlGenerator = new DbSchemaGenerator(diagram)
{
CleanUpDbSchema = dlg.CleanupDbSchema,
UseNavigationPropertyNameForFKeys = dlg.UseNavigationPropertyNameForFKeys,
};
var sb = sqlGenerator.GenerateScripts();
System.IO.File.WriteAllText(dlg.Filename, sb.ToString());
if (dlg.OverwriteDatabase)
{
//Creating a connection to the given database
using (SqlConnection sqlConnection = new SqlConnection(connectionString))
{
var originalDatabase = sqlConnection.Database;
sqlConnection.Open();
//Switching to master database
sqlConnection.ChangeDatabase("master");
ServerConnection svrConnection = new ServerConnection(sqlConnection);
//Recreating database and executing the query file
DropAndRecreateDatabase(originalDatabase, svrConnection);
svrConnection.ExecuteNonQuery(System.IO.File.ReadAllText(dlg.Filename));
}
ModelerTransaction.Enter(() =>
{
//Importing the new schema from database
var sync = new Utilities.DbSchemaImporter(diagram);
sync.FullDatabaseReload = true;
sync.ImportModels();
});
}
System.Windows.Forms.MessageBox.Show("Sql script generation completed.");
}
}
示例4: EjecutarScriptSQLConSmo
private bool EjecutarScriptSQLConSmo(string m_nombreServidor, string m_login, string m_password, string nombreNuevaBase)
{
bool flag;
try
{
if (this.script == "")
{
return false;
}
ServerConnection serverConnection = new ServerConnection
{
ServerInstance = m_nombreServidor
};
if (m_login != "")
{
serverConnection.LoginSecure = false;
serverConnection.Login = m_login;
serverConnection.Password = m_password;
}
serverConnection.Connect();
Server server = new Server(serverConnection);
serverConnection.ExecuteNonQuery(this.script);
flag = true;
}
catch (ConnectionFailureException)
{
MessageBox.Show("Conexi\x00f3n al servidor fallada.");
flag = false;
}
catch (SmoException exception)
{
throw exception;
}
catch (Exception exception2)
{
throw exception2;
}
return flag;
}
示例5: DropAndRecreateDatabase
private void DropAndRecreateDatabase(string databaseName, ServerConnection connection)
{
var commandText =
@"USE [master]
IF EXISTS(SELECT * FROM sys.databases WHERE NAME = '{0}')
BEGIN
ALTER DATABASE [{0}] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
DROP DATABASE [{0}]
END
CREATE DATABASE [{0}]
GO";
connection.ExecuteNonQuery(string.Format(commandText, databaseName));
}