c mysql command对象_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) { //连

源代码:

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 = "潘";

int i = cmd.ExecuteNonQuery();

if (i > 0) 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();

}

}

}

删除执行:

8ef49cfdf29ef1e3a7d238f975540d2c.png

删除后的状态:

b6e8961f5ea1c297e4f4c7c4b06bccad.png

总结:注意cmdtext的正确性

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值