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


C# SqlConnection.ChangeDatabase方法代码示例

本文整理汇总了C#中System.Data.SqlClient.SqlConnection.ChangeDatabase方法的典型用法代码示例。如果您正苦于以下问题:C# SqlConnection.ChangeDatabase方法的具体用法?C# SqlConnection.ChangeDatabase怎么用?C# SqlConnection.ChangeDatabase使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Data.SqlClient.SqlConnection的用法示例。


在下文中一共展示了SqlConnection.ChangeDatabase方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: LookUpEmailBasedOnPhoneName

        protected string LookUpEmailBasedOnPhoneName()
        {
            string phone = TextBox3.Text;
            string name = TextBox2.Text;

            try
            {
                SqlConnection connection = new SqlConnection("Data Source = itksqlexp8; Integrated Security = true");
                connection.Open();
                connection.ChangeDatabase("itk485nnrs");
                string query =
                            "Select email from signuptable where cellPhone = '"+phone+"' and firstname='"+name+"'";
                SqlCommand command = new SqlCommand(query, connection);

                var reader = command.ExecuteReader();
                if (reader.Read())
                {
                    emailFromSignUpTable = reader["email"]+"";
                    connection.Close();
                }
                reader.Close();
            }
            catch(Exception ex)
            {
                Response.Write("Some error occured: "+ex.Message);
            }
            return emailFromSignUpTable;
        }
开发者ID:Nikunj1703,项目名称:SilentAuctionApplication,代码行数:28,代码来源:EnterWinner.aspx.cs

示例2: TestCleanup

        public void TestCleanup()
        {
            var repositoryType = Instance.GetType();

            if (repositoryType == typeof(JsonRepository))
            {
                if (Directory.Exists(JsonWorkingFolder))
                {
                    Directory.Delete(JsonWorkingFolder, recursive: true);
                }
            }
            else if (repositoryType == typeof(SqlRepository))
            {
                using (var cnn = new SqlConnection(SqlConnectionString))
                using (var cmd = new SqlCommand("EXEC sp_MSforeachtable @command1 = 'TRUNCATE TABLE ?'", cnn))
                {
                    cnn.Open();
                    cnn.ChangeDatabase(SqlDatabaseName);
                    cmd.ExecuteNonQuery();
                }
            }
            else if (repositoryType == typeof(MongoRepository))
            {
                var server = new MongoClient(MongoConnectionString).GetServer();
                server.DropDatabase(MongoDatabaseName);
            }
        }
开发者ID:4Rebin,项目名称:NBlog,代码行数:27,代码来源:RepositoryTests.cs

示例3: Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                populateCatagories();
                Session["prevAList"] = "";
                Session["prevBList"] = "";
                        SqlConnection dbConnection = new SqlConnection("Data Source=itksqlexp8;Integrated Security=true");
                //string img= "../UserWebForms/";
                dbConnection.Open();
                 dbConnection.ChangeDatabase("nasa_SilentAuction_v1");
                //string sqlQuery = "select * from item where approvalStatus=" +true;
                SqlCommand cmd = dbConnection.CreateCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "select * from item where approvalStatus= 1";


                //string strQuery = "select * from item where approvalStatus=1";
                //SqlCommand cmd = new SqlCommand(strQuery, dbConnection);

                cmd.CommandType = CommandType.Text;
                //cmd.CommandText = CommandText.te
                cmd.Connection = dbConnection;

                DataTable dt = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                // SqlDataReader pendingListDisplay = cmd.ExecuteReader();
                da.Fill(dt);
                DataList1.DataSource = dt;
                DataList1.DataBind();
                dbConnection.Close();

            }
        }
开发者ID:amannagori,项目名称:SilentAuctionBidding,代码行数:34,代码来源:ViewItems.aspx.cs

示例4: Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            // Important: If didn't check this, the list will get re-populated (and the 1st item will be the selected one)!
            if (!IsPostBack) 
            {
                        SqlConnection dbConnection = new SqlConnection("Data Source=itksqlexp8;Integrated Security=true");

                dbConnection.Open();
               dbConnection.ChangeDatabase("nasa_SilentAuction_v1");
                string SQLString = "SELECT DISTINCT categoryName,categoryPrefix FROM category";

                SqlCommand cmd = new SqlCommand(SQLString, dbConnection);

               

                SqlDataReader ddlValues;
                ddlValues = cmd.ExecuteReader();

                DropDownList1.DataSource = ddlValues;
                DropDownList1.DataValueField = "categoryPrefix";
                DropDownList1.DataTextField = "categoryName";
                DropDownList1.DataBind();

                dbConnection.Close();

            }
        }
开发者ID:amannagori,项目名称:SilentAuctionBidding,代码行数:27,代码来源:DonateItems.aspx.cs

示例5: GetConnection

        public static SqlConnection GetConnection(string sConnection, string sDatabase)
        {
            SqlConnection dbconnection = null;

            try
            {
                dbconnection = new SqlConnection(sConnection);
                dbconnection.Open();

                 if (sDatabase.Trim() != string.Empty)
                {
                    dbconnection.ChangeDatabase(sDatabase);
                }
            }
            catch (System.InvalidOperationException e)
            {
                throw new Exception("GetConnection error: " + e.Message);
            }

            catch (System.Data.SqlClient.SqlException e)
            {
                throw new Exception("GetConnection error2: " + e.Message);
            }

            return dbconnection;
        }
开发者ID:huutruongqnvn,项目名称:vnecoo01,代码行数:26,代码来源:DatabaseUtilities.cs

示例6: BindDataToUserList

        protected void BindDataToUserList()
        {
            try
            {
                        SqlConnection dbConnection = new SqlConnection("Data Source=itksqlexp8;Integrated Security=true");
                dbConnection.Open();
               dbConnection.ChangeDatabase("nasa_SilentAuction_v1");

                String searchString = "select distinct buyerid from item where buyerid is not null";
                SqlCommand SQLString = new SqlCommand(searchString, dbConnection);
                SqlDataAdapter da = new SqlDataAdapter(SQLString);
                DataTable dt = new DataTable();
                da.Fill(dt);
                DropDownList1.DataSource = dt;
                DropDownList1.DataValueField = "buyerid";
                DropDownList1.DataTextField = "buyerid";
                DropDownList1.DataBind();
                dbConnection.Close();

                //Adding "Please select" option in dropdownlist
                DropDownList1.Items.Insert(0, new System.Web.UI.WebControls.ListItem("Select a buyer", ""));
            }
            catch (SqlException exception)
            {
                System.Diagnostics.Debug.WriteLine("<p>Error code " + exception.Number + ": " + exception.Message + "</p>");
            }
        }
开发者ID:amannagori,项目名称:SilentAuctionBidding,代码行数:27,代码来源:ViewInvoices.aspx.cs

示例7: dropDB

        /// <summary>
        /// becareful
        /// </summary>
        /// <param name="connectionString"></param>
        /// <returns></returns>
        public static Boolean dropDB(String connectionString="")
        {
            try
            {
                SqlConnectionStringBuilder pa = new SqlConnectionStringBuilder(connectionString);
                using (SqlConnection sqlconnection = new
            SqlConnection(connectionString))
                {
                    sqlconnection.Open();
                    // if you used master db as Initial Catalog, there is no need to change database
                    sqlconnection.ChangeDatabase("master");

                    string rollbackCommand = @"ALTER DATABASE ["+pa.InitialCatalog+"] SET  SINGLE_USER WITH ROLLBACK IMMEDIATE";

                    SqlCommand deletecommand = new SqlCommand(rollbackCommand, sqlconnection);

                    deletecommand.ExecuteNonQuery();

                    string deleteCommand = @"DROP DATABASE [" + pa.InitialCatalog + "]";

                    deletecommand = new SqlCommand(deleteCommand, sqlconnection);

                    deletecommand.ExecuteNonQuery();
                    return true;
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                return false;
            }
        }
开发者ID:quocdunginfo,项目名称:QuanLyTaiSan,代码行数:37,代码来源:DatabaseHelper.cs

示例8: buttonConnections_Click

        private void buttonConnections_Click(object sender, EventArgs e)
        {
            SqlConnection objCon = new SqlConnection();

            /** option 1: The "HardCoded" way **/
            objCon.ConnectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog = DB1; Integrated Security=True;";

            /** option 2: The "Before .Net v2.0" way**/
            objCon.ConnectionString = ConfigurationSettings.AppSettings["Conn2"];

            /** option 3: The "Common" way  (Note: You must have a reference to the System.Configuration .dll) **/
            objCon.ConnectionString = ConfigurationManager.ConnectionStrings["Conn3"].ConnectionString;

            objCon.StateChange += new StateChangeEventHandler(objCon_StateChange);
            try
            {
                objCon.Open();
                objCon.ChangeDatabase("Northwind");
                MessageBox.Show("Database is now: " + objCon.Database);

            }
            finally
            {
                objCon.Close();
            }
            MessageBox.Show("Database state after Close(): " + objCon.State.ToString());
        }
开发者ID:lorneroy,项目名称:CSharpClass,代码行数:27,代码来源:Form1.cs

示例9: populateDropDown

        void populateDropDown()
        {
                    SqlConnection dbConnection = new SqlConnection("Data Source=itksqlexp8;Integrated Security=true");

            try
            {
                dbConnection.Open();
                dbConnection.ChangeDatabase("nasa_SilentAuction_v1");
                String query = "SELECT * FROM USERS WHERE approvalStatus=1";
                SqlCommand cmd = new SqlCommand(query, dbConnection);

                SqlDataReader data = cmd.ExecuteReader();
                while (data.Read())
                {
                    ListItem u = new ListItem();
                    u.Text = (string)data["userID"];
                    u.Value = (string)data["userID"];
                    buyerIdList.Items.Add(u);
                }
            }
            catch (SqlException exception)
            {
                Response.Write("Error Code: " + exception.Message);
            }
        }
开发者ID:amannagori,项目名称:SilentAuctionBidding,代码行数:25,代码来源:SellItems.aspx.cs

示例10: getBidderDetails

        protected void getBidderDetails()
        {
            string userid = Session["user_id"].ToString();
            try
            {
                        SqlConnection dbConnection = new SqlConnection("Data Source=itksqlexp8;Integrated Security=true");
                dbConnection.Open();
                dbConnection.ChangeDatabase("nasa_SilentAuction_v1");

                String searchString = "select * from users "
                                        + " where userId = @userID; ";

                SqlCommand SQLString = new SqlCommand(searchString, dbConnection);
                SQLString.Parameters.AddWithValue("@userID", userid);

                SqlDataReader idRecords = SQLString.ExecuteReader();
                if (idRecords.Read())
                {
                    bidderName.Text = idRecords["firstName"].ToString().ToUpper();
                    bidderNumber.Text = idRecords["bidder_no"].ToString();
                }

                idRecords.Close();
                dbConnection.Close();
            }
            catch (SqlException exception)
            {
                Response.Write("<p>Error code " + exception.Number
                         + ": " + exception.Message + "</p>");

            }

        }
开发者ID:amannagori,项目名称:SilentAuctionBidding,代码行数:33,代码来源:PrintBidNumber.aspx.cs

示例11: checkIfItemSoldToThisUser

        protected bool checkIfItemSoldToThisUser()
        {
            bool itemSold = false;
            string userid = Session["user_id"].ToString();
            try
            {
                        SqlConnection dbConnection = new SqlConnection("Data Source=itksqlexp8;Integrated Security=true");
                dbConnection.Open();
                dbConnection.ChangeDatabase("nasa_SilentAuction_v1");

                String searchString = "select * from item where buyerID = @userID";

                SqlCommand SQLString = new SqlCommand(searchString, dbConnection);
                SQLString.Parameters.AddWithValue("@userID", userid);

                SqlDataReader idRecords = SQLString.ExecuteReader();
                if (idRecords.Read())
                {
                    itemSold = true;
                }

                idRecords.Close();
                dbConnection.Close();
            }
            catch (SqlException exception)
            {
                Response.Write("<p>Error code " + exception.Number + ": " + exception.Message + "</p>");
            }
            return itemSold;
        }
开发者ID:amannagori,项目名称:SilentAuctionBidding,代码行数:30,代码来源:ViewInvoices.aspx.cs

示例12: updateUserProfile

        protected void updateUserProfile(object sender, EventArgs e)
        {

                    SqlConnection dbConnection = new SqlConnection("Data Source=itksqlexp8;Integrated Security=true");
            try
            {
                dbConnection.Open();
                dbConnection.ChangeDatabase("nasa_SilentAuction_v1");
                string name = Session["user_id"] as string;
             str = "Update users set firstName='" + fname.Text + "', lastName='" + lname.Text + "', addressLine1='" + addressLine1.Text + "', addressLine2='" + addressLine2.Text + "', city='" + city.Text + "', stateName='" + state.Text + "', phoneHome='" + phoneNumber.Text + "', phncarrier='" + networkProvider.SelectedValue + "', email='" + email.Text + "', password='" + Password1.Text + "' WHERE userID='" + userid1.Text + "'";
                SqlCommand sqlCommand = new SqlCommand(str, dbConnection);
              var x =   sqlCommand.ExecuteNonQuery();


                string message = "Bingo! your profile has been Updated. For confirmation we have sent you an email, Happy Bidding :)" ;
                textMessage = new SMSService(networkProvider.Text, phoneNumber.Text, message);
                textMessage.SendTextMessage(textMessage);


                UpdateReply.Text = "Data Updated Successfully, Please check your inbox :)";
               
            }
            catch (SqlException exception)
            {
                Response.Write("<p>Error code " + exception.Number
                    + ": " + exception.Message + "</p>");
            }
        }
开发者ID:amannagori,项目名称:SilentAuctionBidding,代码行数:28,代码来源:UpdateProfile.aspx.cs

示例13: Main

        static void Main(string[] args)
        {
            String sc = "Data Source=.\\sqlexpress;Initial Catalog=MASTER;Integrated Security=true;";

            using (SqlConnection c = new SqlConnection(sc))
            {
                String cmd = "CREATE DATABASE VS2010";

                using (SqlCommand k = new SqlCommand(cmd, c))
                {
                    //SqlCommand faz parte do modelo CONECTADO
                    //ExecuteNonQuery retorna o número de linhas afetadas

                    c.Open();

                    k.ExecuteNonQuery();

                    c.ChangeDatabase("VS2010");

                    k.CommandText = "CREATE TABLE PESSOA (COD_PESSOA INT, NOME_PESSOA VARCHAR(50), SEXO_PESSOA CHAR(1))";

                    k.ExecuteNonQuery();

                    c.Close();
                }
            }

            Console.WriteLine("funcionou");

            Console.ReadKey();
        }
开发者ID:50minutos,项目名称:VS2010,代码行数:31,代码来源:Program.cs

示例14: LookUpForAdminAccess

        protected void LookUpForAdminAccess()
        {
            string adminAccess = "";

            try
            {
                SqlConnection connection = new SqlConnection("Data Source = itksqlexp8; Integrated Security = true");
                connection.Open();
                connection.ChangeDatabase("itk485nnrs");
                string tempEmail = Session["loginEmail"].ToString() ;
                string query =
                            "Select access from signuptable where email = '" + tempEmail + "'";
                SqlCommand command = new SqlCommand(query, connection);

                var reader = command.ExecuteReader();
                if (reader.Read())
                {
                    adminAccess = reader["access"] + "";
                    connection.Close();
                }
                reader.Close();
            }
            catch (Exception ex)
            {
                Response.Write("Some error occured: " + ex.Message);
            }
            Session["adminAccess"] = adminAccess;
        }
开发者ID:Nikunj1703,项目名称:SilentAuctionApplication,代码行数:28,代码来源:BiddingHomescreen.aspx.cs

示例15: Page_Load

 protected void Page_Load(object sender, EventArgs e)
 {
     if (Page.IsPostBack)
     {
         Page.Validate();
         if (Page.IsValid)
         {
             SqlConnection dbConnection = new SqlConnection("Data Source=.\\SQLEXPRESS;Integrated Security=true");
             try
             {
                 dbConnection.Open();
                 dbConnection.ChangeDatabase("ConservationSchool");
                 string SQLString = "SELECT * FROM students WHERE studentID=" + studentID.Text;
                 SqlCommand checkIDTable = new SqlCommand(SQLString, dbConnection);
                 SqlDataReader idRecords = checkIDTable.ExecuteReader();
                 if (idRecords.Read())
                 {
                     Response.Redirect("ReturningStudent.aspx?studentID=" + studentID.Text);
                     idRecords.Close();
                 }
                 else
                 {
                     validateMessage.Text = "<p>**Invalid student ID**</p>";
                     idRecords.Close();
                 }
             }
             catch (SqlException exception)
             {
                 Response.Write("<p>Error code " + exception.Number
                     + ": " + exception.Message + "</p>");
             }
         }
     }
 }
开发者ID:imdigitaljim,项目名称:School_Code,代码行数:34,代码来源:ConservationSchool.aspx.cs


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