int k;
 string updstr="update 基本信息 set 车次='"+dataGrid1[0,0].ToString()+"',出发时间='"+dataGrid1[0,1].ToString()+"',到达时间='"+dataGrid1[0,2].ToString()+"',首发站='"+dataGrid1[0,3].ToString()+"',终点站='"+dataGrid1[0,4].ToString()+"',主要经过='"+dataGrid1[0,5].ToString().Trim()+"',备注='"+dataGrid1[0,6].ToString().Trim()+" ',全长='"+dataGrid1[0,7]+"',经过站数='"+dataGrid1[0,8]+"'  where 车次='"+textBox1.Text.Trim()+"'";
string cs="initial catalog=服务器;data source=(local);integrated security=SSPI;";
SqlConnection myf=new SqlConnection(cs);
myf.Open();
SqlCommand my1=new SqlCommand();
my1.Connection=myf;
my1.CommandType=CommandType.Text;
my1.CommandText=updstr;
k=my1.ExecuteNonQuery();
各位高手,大哥大姐们,帮我看看这段代码那有问题为什么不能对数据库更新呀???

解决方案 »

  1.   

    首页看看你生成的SQL能不能在查询分析器中直接执行
      

  2.   

    估计你写的SELECT错误 先在查询分析器中运行下
      

  3.   

    能执行呀!!
    就是写不到数据库里.是不是我的update那写错了呀!!
      

  4.   

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Data.SqlClient;namespace WindowsApplication1
    {
    /// <summary>
    /// Form6 的摘要说明。
    /// </summary>
    public class Form6 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.DataGrid dataGrid1;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Button button2;
    public SqlDataAdapter da1;
    public DataSet ds;
    public ConnectionState my1;
    private System.Windows.Forms.Label label2;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; public Form6()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if(components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.textBox1 = new System.Windows.Forms.TextBox();
    this.label1 = new System.Windows.Forms.Label();
    this.dataGrid1 = new System.Windows.Forms.DataGrid();
    this.button1 = new System.Windows.Forms.Button();
    this.button2 = new System.Windows.Forms.Button();
    this.label2 = new System.Windows.Forms.Label();
    ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
    this.SuspendLayout();
    // 
    // textBox1
    // 
    this.textBox1.Location = new System.Drawing.Point(152, 16);
    this.textBox1.Name = "textBox1";
    this.textBox1.Size = new System.Drawing.Size(104, 21);
    this.textBox1.TabIndex = 0;
    this.textBox1.Text = "";
    // 
    // label1
    // 
    this.label1.Location = new System.Drawing.Point(32, 16);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(112, 24);
    this.label1.TabIndex = 1;
    this.label1.Text = "您要修改的车次号";
    // 
    // dataGrid1
    // 
    this.dataGrid1.DataMember = "";
    this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
    this.dataGrid1.Location = new System.Drawing.Point(16, 48);
    this.dataGrid1.Name = "dataGrid1";
    this.dataGrid1.Size = new System.Drawing.Size(760, 88);
    this.dataGrid1.TabIndex = 2;
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(288, 16);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(80, 24);
    this.button1.TabIndex = 3;
    this.button1.Text = "找记录";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // button2
    // 
    this.button2.Location = new System.Drawing.Point(400, 16);
    this.button2.Name = "button2";
    this.button2.Size = new System.Drawing.Size(96, 24);
    this.button2.TabIndex = 4;
    this.button2.Text = "确定修改";
    this.button2.Click += new System.EventHandler(this.button2_Click);
    // 
    // label2
    // 
    this.label2.Location = new System.Drawing.Point(528, 8);
    this.label2.Name = "label2";
    this.label2.Size = new System.Drawing.Size(128, 32);
    this.label2.TabIndex = 5;
    this.label2.Text = "label2";
    // 
    // Form6
    // 
    this.AcceptButton = this.button1;
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(792, 149);
    this.Controls.Add(this.label2);
    this.Controls.Add(this.button2);
    this.Controls.Add(this.button1);
    this.Controls.Add(this.dataGrid1);
    this.Controls.Add(this.label1);
    this.Controls.Add(this.textBox1);
    this.Name = "Form6";
    this.Text = "修改记录";
    this.Load += new System.EventHandler(this.Form6_Load);
    ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
    this.ResumeLayout(false); }
    #endregion private void Form6_Load(object sender, System.EventArgs e)
    {

    } private void button1_Click(object sender, System.EventArgs e)
    {
    try
    {
    string cs="initial catalog=服务器;data source=(local);integrated security=SSPI;";
    string str="select * from 基本信息 where 车次='"+textBox1.Text+"'";
    SqlConnection myf=new SqlConnection(cs);
    myf.Open();
    SqlCommand my1=new SqlCommand();
    my1.Connection=myf;
    my1.CommandType=CommandType.Text;
    my1.CommandText=str;
    SqlDataAdapter da1 =new SqlDataAdapter();
    da1.SelectCommand=my1;
    DataSet  ds = new DataSet();
    MessageBox.Show("以找到记录","找到提示");
    da1.Fill(ds,"基本信息");
    //da1列车查询系统.rar.Update(ds,"基本信息");

    dataGrid1.DataSource=ds.Tables[0];
    }
    catch 
    {
    MessageBox.Show("未找到记录","找到提示");
    }
    } private void button2_Click(object sender, System.EventArgs e)
    {
    int k;
     string updstr="update 基本信息 set 车次='"+dataGrid1[0,0].ToString()+"',出发时间='"+dataGrid1[0,1].ToString()+"',到达时间='"+dataGrid1[0,2].ToString()+"',首发站='"+dataGrid1[0,3].ToString()+"',终点站='"+dataGrid1[0,4].ToString()+"',主要经过='"+dataGrid1[0,5].ToString().Trim()+"',备注='"+dataGrid1[0,6].ToString().Trim()+" ',全长='"+dataGrid1[0,7]+"',经过站数='"+dataGrid1[0,8]+"'  where 车次='"+textBox1.Text.Trim()+"'";
    string cs="initial catalog=服务器;data source=(local);integrated security=SSPI;";
    SqlConnection myf=new SqlConnection(cs);
    myf.Open();
    SqlCommand my1=new SqlCommand();
    my1.Connection=myf;
    my1.CommandType=CommandType.Text;
    my1.CommandText=updstr;
    k=my1.ExecuteNonQuery();
    label2.Text="'"+dataGrid1[0,8].ToString()+"'"; }
    }
    }
     
    这是我这个程序的所有代码,我实在是找不到那的错了.
      

  5.   

    是页面没有改过来还是表里面没有修改 不知道你从哪学的 在NET中这样写SELECT的 如果是表里面就没有改 你最好写成参数传递 比较好找一点 如果是表改了 页面没有改 你重新绑定下就可以了
      

  6.   

    修改完数据更新dataGrid显示区域