日期:2014-05-17  浏览次数:20459 次

ExecumentNonQuery()数据库操纵语言
下面语句用于执行DML(INSERT UODATE,DELETE),断点执行发现均未对sql中的表单无任何作用,大家看看是哪里出问题了?
代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
  public partial class Form1 : Form
  {
  public Form1()
  {
  InitializeComponent();
  }

  private void Form1_Load(object sender, EventArgs e)
  {
  // TODO: 这行代码将数据加载到表“fastFoodDataSet.Customer”中。您可以根据需要移动或删除它。
   
  SqlConnection con = new SqlConnection("Data Source=yh-20120602uzvz;Initial Catalog=FastFood;Integrated Security=True");
  string Sql1 = "insert into Customer(CustomerID,UserName,PassWord) values('1001','李宏','111')";
  string Sql2 = "update Customer set name='张伟' where CustomerID=1001";
  string Sql3 = "delete from Customer where CustomerID=1001";
  SqlCommand comm = new SqlCommand(Sql1, con);//创建和声明Command对象
  con.Open();//调用方法前打开数据库连接,可以减少数据库连接所花时间,节省数据库资源
  int count = comm.ExecuteNonQuery();//执行插入SQL语句并返回的int值是命令影响的数据库行数count的值为1
  comm.CommandText = Sql2;//重新设置comm对象要执行Sql语句
  count = comm.ExecuteNonQuery();//执行更新SQL语句并返回的int值是命令影响的数据库行数count的值为1
  comm.CommandText = Sql3;
  count = comm.ExecuteNonQuery();//执行删除SQL语句并返回int值是命令影响的数据库行数count的值为1
  con.Close();//关闭数据库连接


  }

   

   
  }
}


------解决方案--------------------
你的comm里为什么没有comm.connection=con()?

------解决方案--------------------
C# code


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
  public partial class Form1 : Form
  {
  public Form1()
  {
  InitializeComponent();
  }

  private void Form1_Load(object sender, EventArgs e)
  {
  // TODO: 这行代码将数据加载到表“fastFoodDataSet.Customer”中。您可以根据需要移动或删除它。
    
  SqlConnection con = new SqlConnection("Data Source=yh-20120602uzvz;Initial Catalog=FastFood;Integrated Security=True");
  string Sql1 = "insert into Customer(CustomerID,UserName,PassWord) values('1001','李宏','111')";
  string Sql2 = "update Customer set name='张伟' where CustomerID=1001";
  string Sql3 = "delete from Customer where CustomerID=1001";
  SqlCommand comm = new SqlCommand(Sql1, con);//创建和声明Command对象
[color=#FF0000]  //con.Open();//调用方法前打开数据库连接,可以减少数据库连接所花时间,节省数据库资源
  comm.connection=con;
  comm.connection.Open();[/color]
  int count = comm.ExecuteNonQuery();//执行插入SQL语句并返回的int值是命令影响的数据库行数count的值为1
  comm.CommandText = Sql2;//重新设置comm对象要执行Sql语句
  count = comm.ExecuteNonQuery();//执行更新SQL语句并返回的int值是命令影响的数据库行数count的值为1
  comm.CommandText = Sql3;
  count = comm.ExecuteNonQuery();//执行删除SQL语句并返回int值是命令影响的数据库行数count的值为1
  con.Close();//关闭数据库连接


  }

    

    
  }
}