C# SQL Server數據庫操作

1. SQL Server數據庫通過代碼操作: 數據庫創建,表創建,增刪改查操作,可參考下面代碼鏈接:

https://www.c-sharpcorner.com/article/create-a-sql-server-database-dynamically-in-C-Sharp/

其中需要注意的事項爲:

private string ConnectionString ="Integrated Security=SSPI;" +  "Initial Catalog=;" +  Data Source=localhost;";  

Integrated Security=SSPI 爲用當前Windows賬號密碼進行登陸,Initial Catalog=指定數據庫名字,等同於DataBase

 

2. 上面只能當前電腦訪問數據庫,如果想局域網訪問數據庫,或者外網訪問數據庫,可以參考下面步驟來:

首先要單獨創建個數據的登陸賬號,密碼。(我之前用Windows賬號,密碼登陸一直登陸不上,才採用這種方式)

SQL Server 本地新建登陸名,密碼方法如下鏈接:https://blog.csdn.net/SHK242673/article/details/106408489

 

3. 下面爲SQL Server本地局域網連接代碼:

        private string ConnectionString = "SERVER=192.168.10.29;" + "UID=test;" + "PWD=test;" + "DATABASE=;" + "Persist Security Info=True;";
        private string DataBase_Name = "My_DB.mdf";
        private string DataBase_TableName = "my_table";
        private SqlConnection sql_conn = null;
        private SqlCommand sql_cmd = null;

創建數據庫

 private void CreateDataBase(string DataBase_Name)
        {
            // Create a connection  
            sql_conn = new SqlConnection(ConnectionString);

            // Open the connection  
            if (sql_conn.State != ConnectionState.Open)
                sql_conn.Open();

            string sql_createDataBase = "CREATE DATABASE my_db ON PRIMARY"
            + "(Name=my_data, filename='" + DataBase_Name + "', size=10Mb,maxsize=100Mb, filegrowth=1Kb)";

            ExecuteSQLStmt(sql_createDataBase);
            MessageBox.Show("創建數據庫: " + DataBase_Name + " 成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

 private void ExecuteSQLStmt(string sql)
        {
            if (sql_conn.State == ConnectionState.Open)
                sql_conn.Close();

            if (System.IO.File.Exists(DataBase_Name ))
            {
                ConnectionString = "SERVER=192.168.10.29;" + "UID=test;" + "PWD=test;" + "DATABASE=my_db ;" + "Persist Security Info=True;";
            }
            MessageBox.Show("數據庫 connectString: " + ConnectionString + " 成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);

            sql_conn.ConnectionString = ConnectionString;
            sql_conn.Open();
            sql_cmd = new SqlCommand(sql, sql_conn);
            try
            {
                sql_cmd.ExecuteNonQuery();
            }
            catch (SqlException ae)
            {
                //MessageBox.Show(ae.Message.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

創建表:

        private void CreateTable(string Table_Name)
        {   
            // Open the connection  
            if (sql_conn.State == ConnectionState.Open)
                sql_conn.Close();

            ConnectionString = "SERVER=192.168.10.29;" + "UID=test;" + "PWD=test;" + "DATABASE=my_db ;" + "Persist Security Info=True;";
            MessageBox.Show("創建數據表:ConnectionString: " + ConnectionString, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);

            sql_conn.ConnectionString = ConnectionString;
            sql_conn.Open();

            string sql_createTable = "CREATE TABLE " + Table_Name +
                "(taskId BIGINT CONSTRAINT PKeyMyId PRIMARY KEY," +
                "taskName CHAR(300), projectName CHAR(100), brandName CHAR(100))";

            MessageBox.Show("創建數據表:sql_createTable: " + sql_createTable, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            sql_cmd = new SqlCommand(sql_createTable, sql_conn);
            try
            {
                sql_cmd.ExecuteNonQuery();
            }
            catch(SqlException ae)
            {
            }
            MessageBox.Show("創建數據表:" + Table_Name + "成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

查詢數據庫 

private void querry_database(string select_name)

{

string sql_select = "select * from " + DataBase_TableName + " where taskName='" + select_name+ "'";
                sql_cmd = new SqlCommand(sql_select, sql_conn);
                using (SqlDataReader reader = sql_cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {

                        //獲取表裏面的字段信息
                        MessageBox.Show("taskId: " + reader.GetInt64(0) + " taskName: " + reader.GetString(1) + " projectName" + reader.GetString(2) + " brandName: " + reader.GetString(3), "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                     }

            }

}

上面代碼爲運行成功的部分核心代碼,參考這個來,稍微修改就可以用。

突然搞C# SQL Server 有點蒙,先記錄下,給初學者分享下。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章