各位老鸟,小虫我今天解决了一堆问题又来了一堆问题:
这是我做的一个sql的test
通过窗体中的textbox输入数据到数据库中再通过button查看数据这张表,这是代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Common;
using System.Data.SqlClient;
using System.Data.Sql;namespace test1
{
    public partial class testForm : Form
    {
        public testForm()
        {
            InitializeComponent();
        }        
            
               private void testForm_Load(object sender, EventArgs e)
        {        }        private void button_exit_Click(object sender, EventArgs e)
        {
          
        }        private void button_add_Click(object sender, EventArgs e)
        {
            SqlConnection connect = new SqlConnection("server=****; uid=sa;pwd=*****;database=test1");
            connect.Open();            string strSql = "insert table1([Name],[Age],[Duty],[Addr],[Tel]) values(";
            strSql += "'"+this.textBox_name.Text+"'"+this.textBox_age.Text+"'";
            strSql += this.textBox_duty.Text + "'" + this.textBox_addr.Text + "'" + this.textBox_tel.Text + ")";
            SqlCommand cmd = new SqlCommand(strSql, connect);
            
            connect.Close();        }
        private void button_bro_Click(object sender, EventArgs e)
        {
            SqlConnection connect = new SqlConnection("server=*****; uid=sa;pwd=*********;database=test1");
            connect.Open();
            
            string cmdtext = "SELECT * FROM table1";
            SqlCommand cmd = new SqlCommand(cmdtext, connect);
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                Console.WriteLine("{0} from {1}", dr.GetString(0), dr.GetString(1));
            }
            dr.Close();            connect.Close();
        }    }
}
做好后也没有错误,点查看没反应呢
他们说是没绑定数据源,小弟也不知道怎么搞,还请大虾指教!!
谢谢

解决方案 »

  1.   

    private void button_add_Click(object sender, EventArgs e)
            {
                SqlConnection connect = new SqlConnection("server=****; uid=sa;pwd=*****;database=test1");
                connect.Open();            string strSql = "insert table1([Name],[Age],[Duty],[Addr],[Tel]) values(";
                strSql += "'"+this.textBox_name.Text+"'"+this.textBox_age.Text+"'";
                strSql += this.textBox_duty.Text + "'" + this.textBox_addr.Text + "'" + this.textBox_tel.Text + ")";
                SqlCommand cmd = new SqlCommand(strSql, connect);
                cmd.Execute()//加这个,好象是这么写的.
                connect.Close();        }这一段你根本就没有执行command啊
      

  2.   

         private void button_add_Click(object sender, EventArgs e)
            {
                SqlConnection connect = new SqlConnection("server=****; uid=sa;pwd=*****;database=test1");
                connect.Open();            string strSql = "insert table1([Name],[Age],[Duty],[Addr],[Tel]) values(";
                strSql += "'"+this.textBox_name.Text+"'"+this.textBox_age.Text+"'";
                strSql += this.textBox_duty.Text + "'" + this.textBox_addr.Text + "'" + this.textBox_tel.Text + ")";
                SqlCommand cmd = new SqlCommand(strSql, connect);
                cmd.ExecuteNonQuery();
                connect.Close();        }
      

  3.   


    private void button_add_Click(object sender, EventArgs e) 
            { 
                SqlConnection connect = new SqlConnection("server=****; uid=sa;pwd=*****;database=test1"); 
                connect.Open();             string strSql = "insert table1([Name],[Age],[Duty],[Addr],[Tel]) values("; 
                strSql += "'"+this.textBox_name.Text+"'"+this.textBox_age.Text+"'"; 
                strSql += this.textBox_duty.Text + "'" + this.textBox_addr.Text + "'" + this.textBox_tel.Text + ")"; 
                SqlCommand cmd = new SqlCommand(strSql, connect); 
                cmd.ExecuteNonQuery();   //应当是这样吧 
                connect.Close();         } 
    可是运行时候,就在cmd.ExecuteNonQuery();这里有错误
      

  4.   

    insert into 表名([Name],[Age],[Duty],[Addr],[Tel]) values(";