当前位置: 首页>>代码示例>>C#>>正文


C# System.Data.SqlClient.SqlConnection.CreateCommand方法代码示例

本文整理汇总了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;
 }
开发者ID:CurtisRhodes,项目名称:LodgeSecretary,代码行数:32,代码来源:MemberDataSql.cs

示例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
        }
开发者ID:ststeiger,项目名称:AliothBenchmarkDataExtract,代码行数:26,代码来源:Form1.cs

示例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;
 }
开发者ID:CurtisRhodes,项目名称:LodgeSecretary,代码行数:25,代码来源:MasonMasterData.cs

示例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;
        }
开发者ID:ststeiger,项目名称:UpdateIndices,代码行数:31,代码来源:SQL.cs

示例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);
     }
 }
开发者ID:boussaffawalid,项目名称:CutOptima,代码行数:33,代码来源:ExistingDatabaseForm.cs

示例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();
                }
            }
        }
开发者ID:apalmer,项目名称:DatabaseUtility,代码行数:35,代码来源:DbDatabaseSchemaRepository.cs

示例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());
        }
开发者ID:aleksandrzak-rafal,项目名称:AdaptiveCode,代码行数:28,代码来源:AdoNetTradeStorage.cs

示例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();
					}					
				}
			}

		}
开发者ID:NikGovorov,项目名称:nhibernate-core,代码行数:47,代码来源:SampleTest.cs

示例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();
		}
开发者ID:karanbhasin,项目名称:blogEngine.Common,代码行数:10,代码来源:ErrorUtil.cs

示例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();
		}
开发者ID:karanbhasin,项目名称:blogEngine.Common,代码行数:10,代码来源:ErrorUtil.cs

示例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();
         }
     }
 }
开发者ID:kolodkin,项目名称:LobbyHall,代码行数:13,代码来源:Global.asax.cs

示例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();
     }
 }
开发者ID:rrawla,项目名称:SSW.DataOnion.UserGroupDemo,代码行数:13,代码来源:DropDatabase.cs

示例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;
        }
开发者ID:apalmer,项目名称:DatabaseUtility,代码行数:49,代码来源:DbDatabaseSchemaRepository.cs

示例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;
 }
开发者ID:BrownChileWebSolutions,项目名称:HIS_MobileApp,代码行数:17,代码来源:UserService.cs


注:本文中的System.Data.SqlClient.SqlConnection.CreateCommand方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。