ADO.NET之4-使用SqlCommand對象向資料庫添加記錄---ShinePans,

來源:互聯網
上載者:User

ADO.NET之4-使用SqlCommand對象向資料庫添加記錄---ShinePans,

原始碼:


using System;using System.Collections.Generic;using System.Data;using System.Data.SqlClient;using System.Linq;using System.Text;using System.Threading.Tasks;namespace SQLTest{    class Program    {        static void Main(string[] args)        {            //串連資料庫            string connection =                "server=潘尚\\SQLEXPRESS;database=db_test;Trusted_Connection=true";            SqlConnection sc = new SqlConnection();            sc.ConnectionString = connection;            try            {                sc.Open();  //開啟資料庫連接                Console.WriteLine("已經開啟資料庫連接!");                SqlCommand cmd = new SqlCommand();//建立SqlCommand對象                cmd.CommandType = CommandType.Text; //設定執行文本命令                cmd.Connection = sc; //設定對象屬性                cmd.CommandText =                     "INSERT INTO db_student(student_name,student_age,student_address,student_grade)VALUES(@name,@age,@address,@grade)";                //添加參數並為參數賦值                cmd.Parameters.Add("@name", SqlDbType.VarChar, 10).Value = "潘";                cmd.Parameters.Add("@age", SqlDbType.Int).Value = 19;                cmd.Parameters.Add("@address", SqlDbType.VarChar).Value = "武漢";                cmd.Parameters.Add("@grade", SqlDbType.Int).Value = 100;                int i = cmd.ExecuteNonQuery(); //執行資料庫添加記錄命令                if (i > 0) Console.WriteLine("添加記錄成功"); //控制台輸出添加記錄            }            catch (Exception ex)            {                Console.WriteLine("開啟資料庫錯誤:{0}", ex.Message);            }            finally            {                sc.Close();                Console.WriteLine("資料庫連接已關閉!");            }            System.Console.ReadLine();        }    }}


資料庫設計:




運行:




運行之後的資料庫狀態:




簡描述ADONET訪問資料庫的步驟?

1.建立資料庫連接字元創
2.匯入命名空間System.data.sqlcen...
3.jia建立SQLCONNECTION 對象 把連結字元創 放進去
4. 開啟資料庫
5.聲明SQLcommand對象 括弧內放 執行命令的SQL語句 和connection對象

6command對象。方法執行相關命令
 
ADONET技術中,C#通過什的對象可以串連到SQL資料庫

using System.Data;
using System.Data.SqlClient;
..

string strConnection="user id=sa;password=;";
strConnection+="initial catalog=Northwind;Server=YourSQLServer;";
strConnection+="Connect Timeout=30";

SqlConnection objConnection=new SqlConnection(strConnection);
..

objConnection.Open();
objConnection.Close();

strConnection這個變數裡存放的是串連資料庫所需要的連接字串,他指定了要使用的資料提供者和要使用的資料來源.
首先,串連SQL Server使用的命名空間是"System.Data.SqlClient".
其次就是他的連接字串了,我們一個一個參數來介紹(注意:參數間用分號分隔):
 "user id=sa":串連資料庫的驗證使用者名稱為sa.他還有一個別名"uid",所以這句我們還可以寫成"uid=sa".
 "password=":串連資料庫的驗證密碼為空白.他的別名為"pwd",所以我們可以寫為"pwd=".
 這裡注意,你的SQL Server必須已經設定了需要使用者名稱和密碼來登入,否則不能用這樣的方式來登入.如果你的SQL Server設定為Windows登入,那麼在這裡就不需要使用"user id"和"password"這樣的方式來登入,而需要使用"Trusted_Connection=SSPI"來進行登入.
 "initial catalog=Northwind":使用的資料來源為"Northwind"這個資料庫.他的別名為"Database",本句可以寫成"Database=Northwind".
 "Server=YourSQLServer":使用名為"YourSQLServer"的伺服器.他的別名為"Data Source","Address","Addr".如果使用的是本機資料庫且定義了執行個體名,則可以寫為"Server=(local)/執行個體名";如果是遠程伺服器,則將"(local)"替換為遠程伺服器的名稱或IP地址.
 "Connect Timeout=30":連線逾時時間為30秒.

 在這裡,建立連線物件用的建構函式為:SqlConnection.
 

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.