解决方案 »

  1.   

    SqlCommand b = new SqlCommand(sql,con);

    SqlCommand b = new SqlCommand(sql);
    b.Connection=con;你前面定义的sqlstring其实是conString
    不要把数据库连接字符串和SQL语句搞混了最终查询的时候,必须同时把数据库连接字符串和SQL语句给DLL中的函数才行
    你不给,它知道该去上哪个数据库里执行SQL吗
      

  2.   

    按你定义的变量,con应该替换成a
      

  3.   

    SqlConnection是个数据库连接对象
    SqlCommand是个数据库执行对象
    要使用SqlCommand,你得把它跟前面的SqlConnection关联起来才行
      

  4.   

    增加
    b.Connection=con;while (c.Read())之前需要判断c是否为null另外,建议你变量命名的时候,采用有意义的名称
    比如SqlConnection可以命名为_Connection或者其他能够起到自注释功能的变量名称
      

  5.   

    小弟现在将代码改成以下:请大神指教
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;namespace dl
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void button2_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }        private void button1_Click(object sender, EventArgs e)
            {
                string sqlstring = String.Format("server=127.0.0.1;Uid=sa;pwd=sasys;database=DL;");
                SqlConnection a = new SqlConnection(sqlstring);
                a.Open();
                SqlCommand b = new SqlCommand(sqlstring);
                b.Connection=a;
                new SqlCommand("select * from [DL] where name = "+ textBox2.Text+"");
                SqlDataReader c = b.ExecuteReader();                    现在这提示错误:'=' 附近有语法错误
                while (c.Read()) {
                    if (this.textBox1.Text.Equals(c["id"].ToString()) && this.textBox2.Text.Equals(c["name"].ToString())) {
                        MessageBox.Show("正确");
                    }
                    if (!this.textBox1.Text.Equals(c["id"].ToString()) && !this.textBox2.Text.Equals(c["name"].ToString())) {
                        MessageBox.Show("错误");
                    }
                }
            }
        }
    }
      

  6.   

    "select * from [DL] where name = '"+ textBox2.Text+"'"
      

  7.   

    断点调试,把拼接完的SQL语句放到数据库里去执行,看到底怎么了
    或者把拼接好的SQL语句放出来看
      

  8.   

    初学者要多注意sql注入,可以看看SqlParameter,避免漏洞的产生
    http://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqlparameter(v=vs.110).aspx
      

  9.   

    我将我的SQL语句放在数据库中执行了,没有错误,可是一运行程序就说我“'=' 附近有语法错误”
      

  10.   

    你的SQL语句是断点调试,复制出来的,还是你手打的
      

  11.   

    SqlCommand b = new SqlCommand(sqlstring);
                b.Connection=a;
                new SqlCommand("select * from [DL] where name = "+ textBox2.Text+"");
    这是啥语法
    b用的sql语句是前面的数据库连接字符串啊
    而后面的sql语句,new完了没有给b啊
      

  12.   

    所以说让你改变量名,不要瞎起名,连你自己都不知道sqlstring到底是个啥东西了吧
      

  13.   

    界面登录示例
    //登录页面设置
     <table align="center" cellpadding="0" cellspacing="0" class="style1" 
        style="width: 325px">
                <tr>
                    <td class="style2">
                        用&nbsp; 户&nbsp; 名:</td>
                    <td class="style4">
                        <asp:TextBox ID="txtUserName" runat="server" Width="120px"></asp:TextBox>
                    </td>
                    <td class="style3">
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
                            ControlToValidate="txtUserName" ErrorMessage="*"></asp:RequiredFieldValidator>
                    </td>
                </tr>
                <tr>
                    <td class="style2">
                        密&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 码:</td>
                    <td class="style4">
                        <asp:TextBox ID="txtPwd" runat="server" TextMode="Password" Width="120px"></asp:TextBox>
                    </td>
                    <td class="style3">
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
                            ControlToValidate="txtPwd" ErrorMessage="*"></asp:RequiredFieldValidator>
                    </td>
                </tr>
                <tr>
                    <td class="style2">&nbsp;
                        </td>
                    <td class="style4">
                        <asp:Button ID="Button1" runat="server" Text="登录" onclick="Button1_Click" />
    &nbsp;
                        <asp:Button ID="Button2" runat="server" Text="重置" onclick="Button2_Click" />
                    </td>
                    <td class="style3">&nbsp;
                        </td>
                </tr>
            </table>
    //CS页面处理登录事件
    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.Data.SqlClient;public partial class UserLogin : System.Web.UI.UserControl
    {
       protected void Button2_Click(object sender, EventArgs e)
        {
            txtPwd.Text = "";
            txtUserName.Text = "";    }
        protected void Button1_Click(object sender, EventArgs e)//获取Web.config中配置的数据库连接字符串
        {
            string sqlstr = System.Configuration.ConfigurationManager.AppSettings["con"].ToString();
            SqlConnection conn = new SqlConnection(sqlstr);//连接数据库
            conn.Open();                                   //打开数据库
            SqlCommand cmd = new SqlCommand();             //创建SqlCommand对象
            cmd.Connection = conn;                          //设置该对象使用conn连接数据库
            cmd.CommandType = CommandType.Text;             //设置类型
            cmd.CommandText = "select count(*) from Tb_Login where UseName='" + txtUserName.Text.Trim() + "' and UsePassword='"+txtPwd.Text.Trim()+"'";
    //设置sql语句
            int flag = int.Parse(cmd.ExecuteScalar().ToString());//执行sql语句并获取返回值
            if (flag > 0)                      //如果大于0
            {
                Page.ClientScript.RegisterStartupScript(this.Parent.GetType(), "", "alert('登录成功');", true);//说明登陆成功
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(this.Parent.GetType(), "", "alert('登录失败');", true);//否则登陆失败
            }
            conn.Close();//关闭连接
        }
    }