首页 > 代码库 > ADO.NET之5-使用Command对象删除记录---ShinePans

ADO.NET之5-使用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("已经打开数据库连接!");
                string cmdtext = "DELETE FROM db_student WHERE student_name=@name";
                SqlCommand cmd = new SqlCommand(cmdtext, sc);
                cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = http://www.mamicode.com/"潘";>
删除执行:




删除后的状态:





总结:注意cmdtext的正确性