当前位置:编程学习 > C#/ASP.NET >>

代码纠错~简单

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.SqlClient; 

namespace WindowsApplication1 

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

        string connStr = "Data Source=.;Initial Catalog=Car;User ID=sa;Password=;pooling=true"; 

        
        private void label1_Click(object sender, EventArgs e) 
        { 
          
        } 
        private bool InsertTyre() 
        { 
            using (SqlConnection conn = new SqlConnection(connStr)) 
            { 
                conn.Open(); 
                SqlCommand cmd = new SqlCommand(); 
                cmd.CommandText = "INSERT INTO cartyre VALUES(@name,@remains)"; 
                cmd.Connection = conn; 
                cmd.CommandType = CommandType.Text; 
                try 
                { 

                cmd.Parameters.Add(MarkParam("@name", SqlDbType.VarChar, 20, "吉林大学")); 
                cmd.Parameters.Add(MarkParam("@remains", SqlDbType.Int, 4, 50)); 

                cmd.ExecuteNonQuery(); 
                cmd.Parameters.Clear(); 

                cmd.Parameters.Add(MarkParam("@name", SqlDbType.VarChar, 20, "北理工 ")); 
                cmd.Parameters.Add(MarkParam("@remains", SqlDbType.Int, 4, 30)); 

                cmd.ExecuteNonQuery(); 
                cmd.Parameters.Clear(); 
                return true; 
                } 
                catch 
                { 
                    return false; 
                } 
            } 
        } 


        private DataSet ReturnDataSet() 
        { 
            using (SqlConnection conn = new SqlConnection(connStr)) 
            { 
                conn.Open(); 
                SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM cartyre", conn); 
                DataSet ds = new DataSet(); 
                sda.Fill(ds, "ds"); 

                return ds; 
            } 
        } 

        private SqlParameter MarkParam(string name, SqlDbType type, int size, object value) 
        { 
            SqlParameter param = new SqlParameter(name, value); 
            param.Size = size; 
            param.SqlDbType = type; 
            return param; 
        } 

        private void Form1_Load(object sender, EventArgs e) 
        { 
            using (SqlConnection conn = new SqlConnection(connStr)) 
            { 
                conn.Open(); 
                SqlCommand cmd = new SqlCommand(); 
                cmd.CommandText = "SELECT * FROM cartyre"; 
                cmd.CommandType = CommandType.Text; 
                cmd.Connection = conn; 

              
                DataTable dt=new DataTable(); 
                dt.Load(sdr); 
                dataGridView1.DataSource = dt; 
              
            } 
        } 

    } 
}

本人初学,完全不懂,数据老插不进SQL,在GridView也显示不出,不知打出了毛病!望高手赐教! --------------------编程问答--------------------

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.SqlClient; 

namespace WindowsApplication1 

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

        string connStr = "Data Source=.;Initial Catalog=Car;User ID=sa;Password=;pooling=true"; 

        
        private void label1_Click(object sender, EventArgs e) 
        { 
          
        } 
        private bool InsertTyre() 
        { 
            using (SqlConnection conn = new SqlConnection(connStr)) 
            { 
                conn.Open(); 
                SqlCommand cmd = new SqlCommand(); 
                cmd.CommandText = "INSERT INTO cartyre VALUES(@name,@remains)"; 
                cmd.Connection = conn; 
                cmd.CommandType = CommandType.Text; 
                try 
                { 

                cmd.Parameters.Add(MarkParam("@name", SqlDbType.VarChar, 20, "吉林大学")); 
                cmd.Parameters.Add(MarkParam("@remains", SqlDbType.Int, 4, 50)); 

                cmd.ExecuteNonQuery(); 
                cmd.Parameters.Clear(); 

                cmd.Parameters.Add(MarkParam("@name", SqlDbType.VarChar, 20, "北理工 ")); 
                cmd.Parameters.Add(MarkParam("@remains", SqlDbType.Int, 4, 30)); 

                cmd.ExecuteNonQuery(); 
                cmd.Parameters.Clear(); 
                return true; 
                } 
                catch 
                { 
                    return false; 
                } 
            } 
        } 


        private DataSet ReturnDataSet() 
        { 
            using (SqlConnection conn = new SqlConnection(connStr)) 
            { 
                conn.Open(); 
                SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM cartyre", conn); 
                DataSet ds = new DataSet(); 
                sda.Fill(ds, "ds"); 

                return ds; 
            } 
        } 

        private SqlParameter MarkParam(string name, SqlDbType type, int size, object value) 
        { 
            SqlParameter param = new SqlParameter(name, value); 
            param.Size = size; 
            param.SqlDbType = type; 
            return param; 
        } 

        private void Form1_Load(object sender, EventArgs e) 
        { 
            using (SqlConnection conn = new SqlConnection(connStr)) 
            { 
                conn.Open(); 
                SqlCommand cmd = new SqlCommand(); 
                cmd.CommandText = "SELECT * FROM cartyre"; 
                cmd.CommandType = CommandType.Text; 
                cmd.Connection = conn; 

              
                DataTable dt=new DataTable(); 
                dt.Load(sdr); 
                dataGridView1.DataSource = dt; 
                dataGridView1.DataBind();//试试加这句
            } 
        } 

    } 


--------------------编程问答-------------------- 加上dataGridView1.DataBind();

楼上正解,你只指定了数据源DataSource ,却没有执行绑定~ --------------------编程问答-------------------- dataGridView1.DataSource = dt; 在这后面加dataGridView1.DataBind();
要给GridView绑定数据源
--------------------编程问答--------------------  dataGridView1.DataBind(); --------------------编程问答-------------------- DataBind();给gridview绑定数据!! 
补充:.NET技术 ,  ASP.NET
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,