本文整理汇总了C#中System.Data.SqlClient.SqlConnection.CreateCommand方法的典型用法代码示例。如果您正苦于以下问题:C# System.Data.SqlClient.SqlConnection.CreateCommand方法的具体用法?C# System.Data.SqlClient.SqlConnection.CreateCommand怎么用?C# System.Data.SqlClient.SqlConnection.CreateCommand使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Data.SqlClient.SqlConnection
的用法示例。
在下文中一共展示了System.Data.SqlClient.SqlConnection.CreateCommand方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: xxGetMemberDetail
public static Accomplishment xxGetMemberDetail(string memberDetailId, string connString)
{
var memberDetail = new Models.Accomplishment();
try
{
using (var connection = new System.Data.SqlClient.SqlConnection(connString))
{
connection.Open();
using (var command = connection.CreateCommand())
{
command.CommandText = string.Format("Select * from lodge.MemberDetail where MemberDetailId ='{0}'", memberDetailId);
var reader = command.ExecuteReader();
if (reader.HasRows)
{
reader.Read();
{
memberDetail.AccomplishmentId = memberDetailId;
memberDetail.MasonId = (int)reader["MasonId"];
memberDetail.AccomplishmentType = reader["MemberDetailTypeRef"].ToString();
memberDetail.ShortTitle = reader["ShortTitle"].ToString();
memberDetail.Location = reader["Location"].ToString();
memberDetail.StartDate = reader["StartDate"].ToString();
memberDetail.EndDate = reader["EndDate"].ToString();
memberDetail.Comments = reader["Comments"].ToString();
}
}
}
}
}
catch (Exception ex) { memberDetail.Comments = ex.Message; }
return memberDetail;
}
示例2: ExecuteNonQuery
public static void ExecuteNonQuery(string strSQL)
{
System.Data.SqlClient.SqlConnectionStringBuilder csb = new System.Data.SqlClient.SqlConnectionStringBuilder();
csb.DataSource = System.Environment.MachineName;
csb.InitialCatalog = "TestDB";
csb.IntegratedSecurity = true;
string conString = csb.ConnectionString;
using (System.Data.IDbConnection con = new System.Data.SqlClient.SqlConnection(conString))
{
using (System.Data.IDbCommand cmd = con.CreateCommand())
{
cmd.CommandText = strSQL;
if (cmd.Connection.State != System.Data.ConnectionState.Open)
cmd.Connection.Open();
cmd.ExecuteNonQuery();
if (cmd.Connection.State != System.Data.ConnectionState.Closed)
cmd.Connection.Close();
} // End Using cmd
} // End Using con
}
示例3: ChangePassword
public static string ChangePassword(string userName, string password, string newPassword)
{
string encryptedPassword = Encrypt(password);
string encryptedNewPassword = Encrypt(newPassword);
string success = "nogo";
try
{
using (var connection = new System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings["MasonMaster"].ConnectionString))
{
connection.Open();
using (var command = connection.CreateCommand())
{
command.CommandText = string.Format("Update UserProfile set password = '{2}' where UserName='{0}' and Password='{1}'",
userName, encryptedPassword, encryptedNewPassword);
if (command.ExecuteNonQuery() == 1)
success = "ok";
}
}
}
catch (Exception ex)
{
success = ex.Message;
}
return success;
}
示例4: ExecuteNonQuery
public static int ExecuteNonQuery(string strSQL)
{
int retVal = 0;
try
{
using (System.Data.Common.DbConnection con = new System.Data.SqlClient.SqlConnection(GetConnectionString()))
{
using (System.Data.Common.DbCommand cmd = con.CreateCommand())
{
cmd.CommandText = strSQL;
if (cmd.Connection.State != System.Data.ConnectionState.Open)
cmd.Connection.Open();
retVal = cmd.ExecuteNonQuery();
if (cmd.Connection.State != System.Data.ConnectionState.Closed)
cmd.Connection.Close();
} // End Using cmd
}
}
catch (System.Exception ex)
{
// System.Console.WriteLine(ex.Message);
retVal = -1;
}
return retVal;
}
示例5: test_connection_btn_Click
private void test_connection_btn_Click(object sender, EventArgs e)
{
var bldr = new System.Data.SqlClient.SqlConnectionStringBuilder();
bldr.DataSource = serverTextBox.Text;
bldr.InitialCatalog = dbNameTextBox.Text;
bldr.IntegratedSecurity = windows_auth_cb.Checked;
if (sql_auth_cb.Checked)
{
bldr.UserID = username_tb.Text;
bldr.Password = password_tb.Text;
}
bldr.ConnectTimeout = 5;
var conn = new System.Data.SqlClient.SqlConnection(bldr.ConnectionString);
var success = false;
try
{
conn.Open();
var cmd = conn.CreateCommand();
cmd.CommandText = "select 1";
cmd.ExecuteScalar();
success = true;
}
catch (Exception ex)
{
MessageBox.Show("Test connection failed because of an error in initializing provider. " + ex.Message,
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
if (success)
{
MessageBox.Show("Test connection succeeded.", "CutOptima",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
示例6: SaveDatabaseSchema
public override void SaveDatabaseSchema(DatabaseSchema databaseSchema)
{
var connectionString = RepositoryLocation;
using (var connection = new System.Data.SqlClient.SqlConnection(connectionString))
{
connection.Open();
using (var command = connection.CreateCommand())
{
command.CommandText = "DatabaseSchema_Save";
command.CommandType = System.Data.CommandType.StoredProcedure;
command.Parameters.Add(new System.Data.SqlClient.SqlParameter(){
ParameterName = "@databaseName",
SqlDbType = System.Data.SqlDbType.NVarChar,
Direction = System.Data.ParameterDirection.Input,
Value = databaseSchema.Name
});
command.Parameters.Add(new System.Data.SqlClient.SqlParameter(){
ParameterName = "@databaseVersion",
SqlDbType = System.Data.SqlDbType.NVarChar,
Direction = System.Data.ParameterDirection.Input,
Value = databaseSchema.Version.ToString()
});
command.Parameters.Add(new System.Data.SqlClient.SqlParameter(){
ParameterName = "@storedProcedures",
SqlDbType = System.Data.SqlDbType.Structured,
TypeName = "dbo.StoredProcedures",
Value = GetStoredProcedureTable(databaseSchema.StoredProcedures),
});
Int32 returnValue = command.ExecuteNonQuery();
}
}
}
示例7: Persist
public void Persist(IEnumerable<TradeRecord> trades)
{
using (var connection = new System.Data.SqlClient.SqlConnection("Data Source=(local);Initial Catalog=TradeDatabase;Integrated Security=True;"))
{
connection.Open();
using (var transaction = connection.BeginTransaction())
{
foreach (var trade in trades)
{
var command = connection.CreateCommand();
command.Transaction = transaction;
command.CommandType = System.Data.CommandType.StoredProcedure;
command.CommandText = "dbo.insert_trade";
command.Parameters.AddWithValue("@sourceCurrency", trade.SourceCurrency);
command.Parameters.AddWithValue("@destinationCurrency", trade.DestinationCurrency);
command.Parameters.AddWithValue("@lots", trade.Lots);
command.Parameters.AddWithValue("@price", trade.Price);
command.ExecuteNonQuery();
}
transaction.Commit();
}
connection.Close();
}
logger.LogInfo("{0} trades processed", trades.Count());
}
示例8: WithoutUseNHSqlDataProviderWorkProperly
public void WithoutUseNHSqlDataProviderWorkProperly()
{
var createTable = "CREATE TABLE TryDate([Id] [int] IDENTITY(1,1) NOT NULL,[MyDate] [date] NOT NULL)";
var dropTable = "DROP TABLE TryDate";
var insertTable = "INSERT INTO TryDate([MyDate]) VALUES(@p0)";
using(var sqlConnection = new System.Data.SqlClient.SqlConnection(cfg.Properties[Cfg.Environment.ConnectionString]))
{
sqlConnection.Open();
using (var tx = sqlConnection.BeginTransaction())
{
var command = sqlConnection.CreateCommand();
command.Transaction = tx;
command.CommandText = createTable;
command.ExecuteNonQuery();
tx.Commit();
}
try
{
using (var tx = sqlConnection.BeginTransaction())
{
var command = sqlConnection.CreateCommand();
command.Transaction = tx;
command.CommandText = insertTable;
var dateParam = command.CreateParameter();
dateParam.ParameterName = "@p0";
dateParam.DbType = DbType.Date;
dateParam.Value = DateTime.MinValue.Date;
command.Parameters.Add(dateParam);
command.ExecuteNonQuery();
tx.Commit();
}
}
finally
{
using (var tx = sqlConnection.BeginTransaction())
{
var command = sqlConnection.CreateCommand();
command.Transaction = tx;
command.CommandText = dropTable;
command.ExecuteNonQuery();
tx.Commit();
}
}
}
}
示例9: ThrowSqlException
public static void ThrowSqlException() {
System.Data.SqlClient.SqlConnection sqlc = new System.Data.SqlClient.SqlConnection(Foundation.Common.Util.ConfigUtil.GetAppSetting("ChmPortal.databaseConnection"));
sqlc.Open();
System.Data.SqlClient.SqlCommand cmd = sqlc.CreateCommand();
cmd.CommandText = "SELECT * FROM [Table_that_does_not_exist]";
cmd.CommandTimeout = 2;
cmd.ExecuteNonQuery();
cmd.Dispose();
sqlc.Close();
}
示例10: ThrowSqlTimeoutException
public static void ThrowSqlTimeoutException() {
System.Data.SqlClient.SqlConnection sqlc = new System.Data.SqlClient.SqlConnection(Foundation.Common.Util.ConfigUtil.GetAppSetting("ChmPortal.databaseConnection"));
sqlc.Open();
System.Data.SqlClient.SqlCommand cmd = sqlc.CreateCommand();
cmd.CommandText = "Waitfor delay '00:00:05'";
cmd.CommandTimeout = 2;
cmd.ExecuteNonQuery(); // This line will timeout.
cmd.Dispose();
sqlc.Close();
}
示例11: CreateSqlDatabase
public static void CreateSqlDatabase(string dbInitCommand)
{
using (var connection = new System.Data.SqlClient.SqlConnection(
"Data Source=.\\sqlexpress;Initial Catalog=tempdb; Integrated Security=true;User Instance=True;"))
{
connection.Open();
using (var command = connection.CreateCommand())
{
command.CommandText = dbInitCommand;
command.ExecuteNonQuery();
}
}
}
示例12: DropDb
public static void DropDb()
{
using (
var conn =
new System.Data.SqlClient.SqlConnection(
System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
using (var command = conn.CreateCommand())
{
conn.Open();
command.CommandText = "Use Master; Drop Database DataOnion_UserGroupDemo";
command.ExecuteNonQuery();
}
}
示例13: Commit
public void Commit()
{
using (var connection = new System.Data.SqlClient.SqlConnection(_connectionString.FormattedConnectionString))
{
connection.Open();
string sql = String.Join(" ", _entities.Select(e => String.Format("INSERT TestEntities(TestDate , TestInt, TestString) VALUES ('{0}',{1},'{2}')", e.TestDate.ToString("yyyy-MM-ddTHH:mm:ss.fff"), e.TestInt, e.TestString)));
var cmd = connection.CreateCommand();
cmd.CommandText = sql;
cmd.CommandType = System.Data.CommandType.Text;
cmd.ExecuteNonQuery();
}
}
开发者ID:bingdianfeishui,项目名称:StaticVoid.OrmPerformance,代码行数:14,代码来源:InsertSingleCommandTextUnBatched.cs
示例14: LoadDatabaseSchema
public override DatabaseSchema LoadDatabaseSchema(string databaseName, DatabaseVersion version)
{
DatabaseSchema result = new DatabaseSchema();
var connectionString = RepositoryLocation;
using (var connection = new System.Data.SqlClient.SqlConnection(connectionString))
{
connection.Open();
using (var command = connection.CreateCommand())
{
command.CommandText = "DatabaseSchema_Load";
command.CommandType = System.Data.CommandType.StoredProcedure;
command.Parameters.Add(new System.Data.SqlClient.SqlParameter(){
ParameterName = "@databaseName",
SqlDbType = System.Data.SqlDbType.NVarChar,
Direction = System.Data.ParameterDirection.Input,
Value = databaseName
});
command.Parameters.Add(new System.Data.SqlClient.SqlParameter(){
ParameterName = "@databaseVersion",
SqlDbType = System.Data.SqlDbType.NVarChar,
Direction = System.Data.ParameterDirection.Input,
Value = version.ToString()
});
command.Parameters.Add(new System.Data.SqlClient.SqlParameter(){
ParameterName = "@ReturnValue",
Direction = System.Data.ParameterDirection.ReturnValue,
});
using(var reader = command.ExecuteReader())
{
while (reader.Read())
{
result.Name = reader["Name"].ToString();
result.Version = new DatabaseVersion(reader["Version"].ToString());
}
reader.NextResult();
while (reader.Read())
{
Materialize(result, reader);
}
}
}
}
return result;
}
示例15: IsAuthorized
public int IsAuthorized(User objuser)
{
int UserID;
using (System.Data.SqlClient.SqlConnection sqlCon = new System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings["GMapCon"].ConnectionString))
{
using (System.Data.SqlClient.SqlCommand sqlCmd = sqlCon.CreateCommand())
{
sqlCon.Open();
sqlCmd.Connection = sqlCon;
sqlCmd.CommandType = CommandType.Text;
sqlCmd.CommandText = "select isnull(UserID,0) from UserName where UserName='" + objuser.UserName + "' and Password='" + objuser.Password + "'";
UserID = Convert.ToInt32(sqlCmd.ExecuteScalar());
sqlCon.Close();
}
}
return UserID;
}