ADO.NET之6-使用Command修改資料庫中的資料---ShinePans,

來源:互聯網
上載者:User

ADO.NET之6-使用Command修改資料庫中的資料---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(connection);            sc.ConnectionString = connection;            try            {                sc.Open();  //開啟資料庫連接                Console.WriteLine("已經開啟資料庫連接!");                SqlCommand cmd = new SqlCommand("UPDATE db_student SET student_grade=99 where student_name=@name", sc);  //建立SqlCommand對象                cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = "潘";                int i = cmd.ExecuteNonQuery();                if (i > 0) Console.WriteLine("修改成功!");//START:1.刪除資料庫記錄程式碼片段///////////////////////////////////////////////////////               /* string cmdtext = "DELETE FROM db_student WHERE student_name=@name";                SqlCommand cmd = new SqlCommand(cmdtext, sc);                cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = "潘";                int i = cmd.ExecuteNonQuery();                if (i > 0) Console.WriteLine("刪除記錄成功!"); *///END:1.刪除資料庫記錄程式碼片段///////////////////////////////////////////////////////////START:2.添加記錄的代碼///////////////////////////////////////////////////////////////             /*   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("添加記錄成功"); */  //控制台輸出添加記錄 //END:2.添加記錄的代碼/////////////////////////////////////////////////////////////////            }            catch (Exception ex)            {                Console.WriteLine("開啟資料庫錯誤:{0}", ex.Message);            }            finally            {                sc.Close();                Console.WriteLine("資料庫連接已關閉!");            }            System.Console.ReadLine();        }    }}

運行修改:




修改之後的資料庫:




怎使用ADONET向資料庫中插入資料

不知道你有沒有用三層,所以就寫這樣了。public class Student
{
private int studentid; public int Studentid
{
get { return studentid; }
set { studentid = value; }
}
private string studentno; public string Studentno
{
get { return studentno; }
set { studentno = value; }
}
private string studentname; public string Studentname
{
get { return studentname; }
set { studentname = value; }
}
private string gender; public string Gender
{
get { return gender; }
set { gender = value; }
}
private DateTime birthday; public DateTime Birthday
{
get { return birthday; }
set { birthday = value; }
}
private int classid; public int Classid
{
get { return classid; }
set { classid = value; }
}
private int status; public int Status
{
get { return status; }
set { status = value; }
}
private string remark; public string Remark
{
get { return remark; }
set { remark = value; }
}
} 下面的是擷取介面輸入的值:namespace student{
Student stu = new Student(); private void btnCreate_Click(object sender, EventArgs e)
{
stu.Studentname = this.txtName.Text;
st......餘下全文>>
 
簡描述ADONET訪問資料庫的步驟?

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

6command對象。方法執行相關命令
 

相關文章

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.