本文整理汇总了C#中System.Data.SqlClient.SqlConnection.ExecuteNonQuery方法的典型用法代码示例。如果您正苦于以下问题:C# SqlConnection.ExecuteNonQuery方法的具体用法?C# SqlConnection.ExecuteNonQuery怎么用?C# SqlConnection.ExecuteNonQuery使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Data.SqlClient.SqlConnection
的用法示例。
在下文中一共展示了SqlConnection.ExecuteNonQuery方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ExecWithoutDB
static void ExecWithoutDB(string sql)
{
SqlConnection.ClearAllPools();
using (var connection = new SqlConnection(@"Data Source=(localdb)\ProjectsV12;Integrated Security=True")) {
connection.Open();
connection.ExecuteNonQuery(sql);
}
}
示例2: ExecuteNonQuery
public void ExecuteNonQuery()
{
string sql = "DECLARE @FizzBuzz VARCHAR(MAX) = @Fizz";
var dict = new Dictionary<string, object> {{"@Fizz", 1}};
// Examples
using (var conn = new SqlConnection(My.Config.ConnectionString.UnitTest.ConnectionString))
{
conn.Open();
conn.ExecuteNonQuery(sql, dict.ToDbParameters(conn));
}
}
示例3: RemoveDoc
public void RemoveDoc(CaseDoc doc)
{
using (var connection = new SqlConnection(_connectionString))
{
connection.Open();
var prKey1 = new KeyValuePair<string, object>("案件编号", doc.案件编号);
var prKey2 = new KeyValuePair<string, object>("文件名", doc.FileName);
string command = string.Format("DELETE FROM [{0}] WHERE {1} = @{1} AND {2}[email protected]{2}", CaseDocTableName,
prKey1.Key, prKey2.Key);
connection.ExecuteNonQuery(command);
}
}
示例4: RemoveBookmarkRecord
public void RemoveBookmarkRecord(string bookmarkName)
{
using (var connection = new SqlConnection(_connectionString))
{
connection.Open();
string command = string.Format("DELETE FROM [{0}] WHERE {1} = @{1} AND {2} = @{2}",
CaseWorkflowManager.BookmarkTableName,
CaseWorkflowManager.CaseIdColumnName,
CaseWorkflowManager.BookmarkNameColumnName);
var parameters = new[]
{
new SqlParameter("@" + CaseWorkflowManager.CaseIdColumnName, _caseId),
new SqlParameter("@" + CaseWorkflowManager.BookmarkNameColumnName, bookmarkName)
};
connection.ExecuteNonQuery(command, parameters);
}
}
示例5: TestErrorSuccess
public void TestErrorSuccess()
{
// Arrange
var guid = Guid.NewGuid();
var logger = new OneHydraLogger("MyTestLogger"); var helper = new ConfigManagerHelper();
var connString = helper.GetConnectionString("OneHydraLog");
var query = string.Format(@"
SELECT [Id], [Date], [Thread], [Level], [Logger], [Message], [Exception], [MachineName]
FROM [dbo].[OneSearchLog]
WHERE Message LIKE {0}", guid.ForSql());
// Act
logger.Error(guid.ToString(), new Exception("Test exception"));
// Clean up
dynamic logRow;
using (var conn = new SqlConnection(connString))
{
logRow = conn.GetObject(query,
r =>new
{
Id = r.GetInt32(0),
Date = r.GetDateTime(1),
Thread = r.GetString(2),
Level = r.GetString(3),
Logger = r.GetString(4),
Message = r.GetString(5),
Exception = r.GetString(6),
MachineName = r.GetString(7)
});
conn.ExecuteNonQuery(string.Format("DELETE FROM [dbo].[OneSearchLog] WHERE Message LIKE {0} ", guid.ForSql()));
}
// Assert
Assert.IsNotNull(logRow);
Assert.AreEqual(guid.ToString(), logRow.Message);
Assert.AreEqual("Test exception\r\n", logRow.Exception);
Assert.AreEqual(Environment.MachineName, logRow.MachineName);
Assert.AreEqual("ERROR", logRow.Level);
Assert.AreEqual("MyTestLogger", logRow.Logger);
}
示例6: ExecuteEntities
public void ExecuteEntities()
{
var date = new DateTime(1981, 04, 13);
Guid guid = Guid.NewGuid();
var entity = new EntityWithAllColumn();
entity.BitIntColumn = 1;
entity.BinaryColumn = new byte[] {1};
entity.BitColumn = true;
entity.CharColumn = "z";
entity.DateColumn = date;
entity.DateTimeColumn = date;
entity.DateTime2Column = date;
entity.DateTimeOffsetColumn = date;
entity.DecimalColumn = 1.25m;
entity.FloatColumn = 1.25f;
entity.ImageColumn = new byte[] {1};
entity.IntColumn = 1;
entity.MoneyColumn = 1.25m;
entity.NCharColumn = "z";
entity.NTextColumn = "z";
entity.NumericColumn = 1;
entity.NVarcharColumn = "z";
entity.NVarcharMaxColumn = "z";
entity.RealColumn = 1.25f;
entity.SmallDateTimeColumn = date;
entity.SmallIntColumn = null;
entity.SmallMoneyColumn = 1.25m;
entity.TextColumn = "z";
entity.TimeColumn = date.TimeOfDay;
entity.TimestampColumn = new byte[] {1};
entity.TinyIntColumn = 1;
entity.UniqueIdentifierColumn = guid;
entity.VarBinaryColumn = new byte[] {1};
entity.VarBinaryMaxColumn = new byte[] {1};
entity.VarcharColumn = "z";
entity.VarcharMaxColumn = "z";
entity.XmlColumn = "z";
using (var conn = new SqlConnection(My.Config.ConnectionString.UnitTest.ConnectionString))
{
conn.Open();
conn.ExecuteNonQuery("TRUNCATE TABLE EntityWithAllColumns");
var dt = new DataTable();
using (var copy = new SqlBulkCopy(conn))
{
foreach (PropertyInfo property in entity.GetType().GetProperties())
{
if (property.Name == "SmallIntColumn")
{
dt.Columns.Add(property.Name, typeof (short));
}
else
{
dt.Columns.Add(property.Name, property.PropertyType);
}
copy.ColumnMappings.Add(property.Name, property.Name);
}
DataRow dr = dt.NewRow();
dt.Rows.Add(dr);
foreach (PropertyInfo property in entity.GetType().GetProperties())
{
object value = property.GetValue(entity, null);
dr[property.Name] = value == null ? DBNull.Value : value;
}
copy.DestinationTableName = "EntityWithAllColumns";
copy.WriteToServer(dt);
}
// Unit Test
EntityWithAllColumn result = conn.ExecuteEntities<EntityWithAllColumn>("SELECT TOP 1 * FROM EntityWithAllColumns").ToList()[0];
Assert.AreEqual(1, result.BitIntColumn);
Assert.AreEqual(1, result.BinaryColumn[0]);
Assert.AreEqual(true, result.BitColumn);
Assert.AreEqual("z", result.CharColumn);
Assert.AreEqual(date, result.DateColumn);
Assert.AreEqual(date, result.DateTimeColumn);
Assert.AreEqual(date, result.DateTime2Column);
Assert.AreEqual(date, result.DateTimeOffsetColumn);
Assert.AreEqual(1.25m, result.DecimalColumn);
Assert.AreEqual(1.25f, result.FloatColumn);
Assert.AreEqual(1, result.ImageColumn[0]);
Assert.AreEqual(1, result.IntColumn);
Assert.AreEqual(1.25m, result.MoneyColumn);
Assert.AreEqual("z", result.NCharColumn);
Assert.AreEqual("z", result.NTextColumn);
Assert.AreEqual(1, result.NumericColumn);
Assert.AreEqual("z", result.NVarcharColumn);
Assert.AreEqual("z", result.NVarcharMaxColumn);
Assert.AreEqual(1.25f, result.RealColumn);
Assert.AreEqual(date, result.SmallDateTimeColumn);
Assert.AreEqual(null, result.SmallIntColumn);
Assert.AreEqual(1.25m, result.SmallMoneyColumn);
Assert.AreEqual("z", result.TextColumn);
Assert.AreEqual(date.TimeOfDay, result.TimeColumn);
//.........这里部分代码省略.........
示例7: DeleteActivity
public bool DeleteActivity(int id)
{
var sql = "DELETE FROM ActivityActivity WHERE ParentActivityId = " + id + " or ActivityId = " + id
+ "; DELETE FROM RoleActivity WHERE ActivityId = " + id
+ "; DELETE FROM Activity WHERE Id = " + id;
using (var conn = new SqlConnection(ConnectionString))
{
conn.Open();
using(var transaction = conn.BeginTransaction())
{
conn.ExecuteNonQuery(sql, null, CommandType.Text, transaction);
transaction.Commit();
}
}
return true;
}
示例8: DeleteUserAuthorisation
public bool DeleteUserAuthorisation(int id)
{
var sql = "DELETE FROM UserRole WHERE Id = " + id
+ "; DELETE FROM UserAuthorisation WHERE Id = " + id;
using (var conn = new SqlConnection(ConnectionString))
{
conn.Open();
using (var transaction = conn.BeginTransaction())
{
conn.ExecuteNonQuery(sql, null, CommandType.Text, transaction);
transaction.Commit();
}
}
return true;
}