本文整理汇总了C#中SqlCommand.ExecuteNonQuery方法的典型用法代码示例。如果您正苦于以下问题:C# SqlCommand.ExecuteNonQuery方法的具体用法?C# SqlCommand.ExecuteNonQuery怎么用?C# SqlCommand.ExecuteNonQuery使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SqlCommand
的用法示例。
在下文中一共展示了SqlCommand.ExecuteNonQuery方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DeleteAudit
public void DeleteAudit(int AuditID)
{
string SQL = "DELETE FROM tblAuditTrail WHERE AuditID = '" + AuditID + "'";
SqlCommand scmDelete = new SqlCommand(SQL, con);
scmDelete.ExecuteNonQuery();
scmDelete.Dispose();
}
示例2: BasicParallelTest
private void BasicParallelTest(string connectionString, string tempTableName)
{
using (var connection = new SqlConnection(connectionString))
{
connection.Open();
SqlTransaction trans1 = connection.BeginTransaction();
SqlTransaction trans2 = connection.BeginTransaction();
SqlTransaction trans3 = connection.BeginTransaction();
SqlCommand com1 = new SqlCommand("select top 1 au_id from " + tempTableName, connection);
com1.Transaction = trans1;
com1.ExecuteNonQuery();
SqlCommand com2 = new SqlCommand("select top 1 au_id from " + tempTableName, connection);
com2.Transaction = trans2;
com2.ExecuteNonQuery();
SqlCommand com3 = new SqlCommand("select top 1 au_id from " + tempTableName, connection);
com3.Transaction = trans3;
com3.ExecuteNonQuery();
trans1.Rollback();
trans2.Rollback();
trans3.Rollback();
com1.Dispose();
com2.Dispose();
com3.Dispose();
}
}
示例3: ExecuteNonQuery
public static int ExecuteNonQuery(this SqlConnection connection, string sprocName, params SqlParameter[] sqlParameters)
{
using (var sqlCommand = new SqlCommand(sprocName, connection))
{
return sqlCommand.ExecuteNonQuery();
}
}
示例4: XmlTest
public static void XmlTest()
{
string tempTable = "xml_" + Guid.NewGuid().ToString().Replace('-', '_');
string initStr = "create table " + tempTable + " (xml_col XML)";
string insertNormStr = "INSERT " + tempTable + " VALUES('<doc>Hello World</doc>')";
string insertParamStr = "INSERT " + tempTable + " VALUES(@x)";
string queryStr = "select * from " + tempTable;
using (SqlConnection conn = new SqlConnection(DataTestUtility.TcpConnStr))
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = initStr;
cmd.ExecuteNonQuery();
try
{
cmd.CommandText = insertNormStr;
cmd.ExecuteNonQuery();
SqlCommand cmd2 = new SqlCommand(insertParamStr, conn);
cmd2.Parameters.Add("@x", SqlDbType.Xml);
XmlReader xr = XmlReader.Create("data.xml");
cmd2.Parameters[0].Value = new SqlXml(xr);
cmd2.ExecuteNonQuery();
cmd.CommandText = queryStr;
using (SqlDataReader reader = cmd.ExecuteReader())
{
int currentValue = 0;
string[] expectedValues =
{
"<doc>Hello World</doc>",
"<NewDataSet><builtinCLRtypes><colsbyte>1</colsbyte><colbyte>2</colbyte><colint16>-20</colint16><coluint16>40</coluint16><colint32>-300</colint32><coluint32>300</coluint32><colint64>-4000</colint64><coluint64>4000</coluint64><coldecimal>50000.01</coldecimal><coldouble>600000.987</coldouble><colsingle>70000.9</colsingle><colstring>string variable</colstring><colboolean>true</colboolean><coltimespan>P10675199DT2H48M5.4775807S</coltimespan><coldatetime>9999-12-30T23:59:59.9999999-08:00</coldatetime><colGuid>00000001-0002-0003-0405-060708010101</colGuid><colbyteArray>AQIDBAUGBwgJCgsMDQ4PEA==</colbyteArray><colUri>http://www.abc.com/</colUri><colobjectsbyte xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"byte\">1</colobjectsbyte><colobjectbyte xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"unsignedByte\">2</colobjectbyte><colobjectint16 xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"short\">-20</colobjectint16><colobjectuint16 xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"unsignedShort\">40</colobjectuint16><colobjectint32 xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"int\">-300</colobjectint32><colobjectuint32 xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"unsignedInt\">300</colobjectuint32><colobjectint64 xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"long\">-4000</colobjectint64><colobjectuint64 xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"unsignedLong\">4000</colobjectuint64><colobjectdecimal xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"decimal\">50000.01</colobjectdecimal><colobjectdouble xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"double\">600000.987</colobjectdouble><colobjectsingle xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"float\">70000.9</colobjectsingle><colobjectstring xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"string\">string variable</colobjectstring><colobjectboolean xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"boolean\">true</colobjectboolean><colobjecttimespan xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"duration\">P10675199DT2H48M5.4775807S</colobjecttimespan><colobjectdatetime xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"dateTime\">9999-12-30T23:59:59.9999999-08:00</colobjectdatetime><colobjectguid xmlns:msdata=\"urn:schemas-microsoft-com:xml-msdata\" msdata:InstanceType=\"System.Guid\">00000001-0002-0003-0405-060708010101</colobjectguid><colobjectbytearray xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"base64Binary\">AQIDBAUGBwgJCgsMDQ4PEA==</colobjectbytearray><colobjectUri xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"anyURI\">http://www.abc.com/</colobjectUri></builtinCLRtypes><builtinCLRtypes><colbyte>2</colbyte><colint16>-20</colint16><colint32>-300</colint32><coluint32>300</coluint32><coluint64>4000</coluint64><coldecimal>50000.01</coldecimal><coldouble>600000.987</coldouble><colsingle>70000.9</colsingle><colboolean>true</colboolean><colobjectsbyte xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"byte\">11</colobjectsbyte><colobjectbyte xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"unsignedByte\">22</colobjectbyte><colobjectint16 xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"short\">-200</colobjectint16><colobjectuint16 xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"unsignedShort\">400</colobjectuint16><colobjectint32 xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"int\">-3000</colobjectint32><colobjectuint32 xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"unsignedInt\">3000</colobjectuint32><colobjectint64 xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"long\">-40000</colobjectint64><colobjectuint64 xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"unsignedLong\">40000</colobjectuint64><colobjectdecimal xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"decimal\">500000.01</colobjectdecimal><colobjectdouble xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"double\">6000000.987</colobjectdouble><colobjectsingle xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"float\">700000.9</colobjectsingle><colobjectstring xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"string\">string variable 2</colobjectstring><colobjectboolean xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"boolean\">false</colobjectboolean><colobjecttimespan xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"duration\">-P10675199DT2H48M5.4775808S</colobjecttimespan><colobjectdatetime xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"dateTime\">0001-01-01T00:00:00.0000000-08:00</colobjectdatetime><colobjectguid xmlns:msdata=\"urn:schemas-microsoft-com:xml-msdata\" msdata:InstanceType=\"System.Guid\">00000002-0001-0001-0807-060504030201</colobjectguid><colobjectbytearray xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"base64Binary\">EA8ODQwLCgkIBwYFBAMCAQ==</colobjectbytearray></builtinCLRtypes></NewDataSet>"
};
while (reader.Read())
{
Assert.True(currentValue < expectedValues.Length, "ERROR: Received more values than expected");
SqlXml sx = reader.GetSqlXml(0);
xr = sx.CreateReader();
xr.Read();
DataTestUtility.AssertEqualsWithDescription(expectedValues[currentValue++], xr.ReadOuterXml(), "FAILED: Did not receive expected data");
}
}
}
finally
{
cmd.CommandText = "drop table " + tempTable;
cmd.ExecuteNonQuery();
}
}
}
示例5: AddOrder
public void AddOrder(int Quantity, string Username, string ProductName, string OrderStatus, string Contact, string Address)
{
string SQL = "INSERT INTO tblOrders (Quantity, Username, ProductName, Date, OrderStatus, Contact, Address) VALUES " +
"(" + "'" + Quantity + "'" + ", " + "'" + Username + "'" + ", " + "'" + ProductName + "'" + ", " + "GETDATE()" + ", " + "'" + OrderStatus + "'" + ", " + "'" + Contact + "'" + ", " + "'" + Address + "'" + ")";
SqlCommand scmAdd = new SqlCommand(SQL, con);
scmAdd.ExecuteNonQuery();
scmAdd.Dispose();
}
示例6: Register
public void Register(string Name, string UserName, string Password, string UserType)
{
string SQL = "INSERT INTO TblUser (Name, UserName, Password, UserType) VALUES " +
"(" + "'" + Name + "'" + ", " + "'" + UserName + "'" + ", " + "'" + Password + "'" + ", " + "'" + UserType + "'" + ")";
SqlCommand scmAdd = new SqlCommand(SQL, con);
scmAdd.ExecuteNonQuery();
scmAdd.Dispose();
}
示例7: AuditTrail
public void AuditTrail(int UID, string Action)
{
string SQL = "INSERT INTO tblAuditTrail (UserID, Action, DateAction) VALUES " +
"(" + UID + ",'" +
Action + "', GETDATE())";
SqlCommand scmAdd = new SqlCommand(SQL, con);
scmAdd.ExecuteNonQuery();
scmAdd.Dispose();
}
示例8: DropExistingTable
private void DropExistingTable()
{
using (var connection = SqlServerConnection.Create())
using (var command = new SqlCommand("IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = SCHEMA_NAME() AND TABLE_NAME = 'Snapshot') DROP TABLE [Snapshot];", connection))
{
connection.Open();
command.ExecuteNonQuery();
}
}
示例9: Execute
//use for UPDATE, DELETE
public static void Execute(string SqlStatement)
{
SqlConnection con = new SqlConnection(ConnectionString);
con.Open();
SqlCommand com = new SqlCommand(SqlStatement, con);
com.ExecuteNonQuery();
con.Close(); //dagdag
}
示例10: ExecuteNonQuery
public static int ExecuteNonQuery(string sql, params SqlParameter[] ps)
{
using (SqlConnection connection = new SqlConnection(connStr))
{
SqlCommand command = new SqlCommand(sql, connection);
command.Parameters.AddRange(ps);
connection.Open();
return command.ExecuteNonQuery();
}
}
示例11: AddData
public static AddData(string query)
{
using (var scon = Connections.Connect())
{
SqlCommand add = new SqlCommand(query, scon);
add.ExecuteNonQuery();
scon.Close();
}
return 1;
}
示例12: EditOrder
public void EditOrder(int OrderID, string ProductName, int Quantity, string Contact, string Address)
{
string SQL = "UPDATE tblOrders SET" +
" ProductName = '" + ProductName + "', " +
"Quantity = '" + Quantity + "', " +
"Contact = '" + Contact + "', " +
"Address = '" + Address + "'" +
"WHERE OrderID = '" + OrderID + "'";
SqlCommand scmEdit = new SqlCommand(SQL, con);
scmEdit.ExecuteNonQuery();
scmEdit.Dispose();
}
示例13: SelfContainedUpdateData
public static void SelfContainedUpdateData(Library.TransactionWrapper sender, Library.TransactionArgs args)
{
// TODO: Paramter checks
AggregateTransactionedOperation operation = args.Operation as AggregateTransactionedOperation;
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.Transaction = args.Transaction as System.Data.SqlClient.SqlTransaction;
sqlCommand.Connection = args.Connection as System.Data.SqlClient.SqlConnection;
sqlCommand.CommandText = ""; /* Some suitable text here */
sqlCommand.ExecuteNonQuery();
}
示例14: AddMerchants
// Insert Methods (will call the connection method)
public static String AddMerchants(string merch)
{
using (var scon = SqlInserts.Connect())
{
SqlCommand add = new SqlCommand("INSERT INTO MerchantTracking (MerchantName) SELECT @1", scon);
add.Parameters.Add(new SqlParameter("@1", merch));
try
{
add.ExecuteNonQuery();
scon.Close();
return "Merchant added!";
}
catch
{
scon.Close();
return "Failed to add merchant";
}
}
}
示例15: create
// Create User <user> on database
public static bool create(String username, String password, bool supervisor)
{
try
{
SqlConnection sqlConnection1 = new SqlConnection(Globals.connectionString);
SqlCommand cmd = new SqlCommand();
// TODO Figure out how to use parameters instead of direct replacement (DANGEROUS!!!!)
cmd.CommandText = @"CREATE LOGIN {UNAME} WITH PASSWORD = '{PASSWORD}';".Replace("{UNAME}", username).Replace("{PASSWORD}", password);
cmd.Connection = sqlConnection1;
//cmd.Parameters.Add("{UNAME}", SqlDbType.VarChar);
//cmd.Parameters.Add("{Password}", SqlDbType.VarChar);
//cmd.Parameters["{UNAME}"].Value = username;
//cmd.Parameters["{Password}"].Value = password;
sqlConnection1.Open();
int result = cmd.ExecuteNonQuery();
// TODO Figure out how to use parameters instead of direct replacement (DANGEROUS!!!!)
cmd.CommandText = @"EXEC master..sp_addsrvrolemember @loginame = N'{UNAME}', @rolename = N'sysadmin';".Replace("{UNAME}", username);
//cmd.Parameters.Add("{UNAME}", SqlDbType.VarChar);
//cmd.Parameters["{UNAME}"].Value = username;
cmd.ExecuteNonQuery();
if (supervisor)
{
cmd.CommandText = @"EXEC master..sp_addsrvrolemember @loginame = N'{UNAME}', @rolename = N'bulkadmin';".Replace("{UNAME}", username);
//cmd.Parameters.Add("{UNAME}", SqlDbType.VarChar);
//cmd.Parameters["{UNAME}"].Value = username;
cmd.ExecuteNonQuery();
}
sqlConnection1.Close();
return result == -1;
}
catch (Exception ex)
{
return false;
}
}