当前位置:编程学习 > 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();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            try
            {
                string ConStr = "server=(local);user id=sa;pwd=;database=dada_2010";

                SqlConnection con = new SqlConnection(ConStr);
                StringBuilder sb = new StringBuilder();
                sb.Append(@"select * from ,mertrial");
                string SqlStr = sb.ToString() + "where Convert(datetime,Convert(varchar(10),DATE,120)) between'" + ((Form1)this.Owner).dateTimePicker1.Value.ToShortDateString() + "'and'" + ((Form1this.Owner).dateTimePicker2.Value.ToShortDateString() + "'";
                SqlDataAdapter ada = new SqlDataAdapter(SqlStr, con);
                DataSet ds = new DataSet();
                ada.Fill(ds);
                this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
            }
            catch
            {
                return;
            }
        }
    }
}

请教下这段代码有没有问题 为什么我获取不到数据。。 --------------------编程问答-------------------- 此处SqlDataAdapter ada = new SqlDataAdapter(SqlStr, con); 
设置断点,单步调试一下
看SqlStr是什么东东,然后在数据库中执行一下,看是不是有数据。 --------------------编程问答-------------------- 恩,UP --------------------编程问答-------------------- 把sql语句放到数据库里执行…… --------------------编程问答-------------------- private void button1_Click(object sender, EventArgs e) 
        { 

            try 
            { 
                string ConStr = "server=(local);user id=sa;pwd=;database=dada_2010"; 

                SqlConnection con = new SqlConnection(ConStr); 
                StringBuilder sb = new StringBuilder(); 
                sb.Append(@"select * from mertrial"); 
                string SqlStr = sb.ToString() + "where Convert(datetime,Convert(varchar(10),DATE,120)) between'" + ((Form1)this.Owner).dateTimePicker1.Value.ToShortDateString() + "'and'" + ((Form1this.Owner).dateTimePicker2.Value.ToShortDateString() + "'"; 
                SqlDataAdapter ada = new SqlDataAdapter(SqlStr, con); 
                DataSet ds = new DataSet(); 
                ada.Fill(ds); 
                this.dataGridView1.DataSource = ds.Tables[0].DefaultView; 
            } 
            catch 
            { 
                return; 
            } 
        } 
--------------------编程问答--------------------   sb.Append(@"select * from ,mertrial"); 
这个,号起啥作用滴??
--------------------编程问答-------------------- 你的就是button1_Click事件,从数据库里面取出数据填充在GridView里面


    public void Button1_Click(object sender, EventArgs e)
        {
            string str = @"server=.\SQLEXPRESS;database=db_CSManage;uid=sa;pwd=sa";
            SqlConnection Conn = new SqlConnection(str);
            string sql = "select * from mertrial";
            SqlDataAdapter sd = new SqlDataAdapter(sql, Conn);
            DataSet ds = new DataSet();
            sd.Fill(ds);

            this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
        }


这样呢? --------------------编程问答--------------------
引用 5 楼 wanghui0380 的回复:
  sb.Append(@"select * from,mertrial");
这个,号起啥作用滴??

嗯那个逗号 代码里没有的 ,用6楼的那个是可以的  
但我想根据这个DATE 字段来查询出相匹配的数据 用我那个代码 就查不出来任何数据 请教下如何弄 谢谢大家
--------------------编程问答-------------------- 继续顶一下~~~ --------------------编程问答-------------------- 还得继续顶顶
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,