解决方案 »
- 程序用的是SQL2000,我现在装的是SQL2008,这。。。
- 请问Controls索引
- 是不是一个对象在 console.writeline()中输出,都是默认输出对象的 toString()方法?
- 鼠标不能移动
- DrawImageUnscaled()绘制图像到打印预览变的模糊
- 关于读取XML
- 我在学习ASP.NET 2.0, 要用C#编写其程序代码, 我的问题是: 不需要Main()函数了吗?
- C#里面如何将.txt文件中的数据读入,
- c#有无模式对话框
- combox绑定dataview,为什么两个dataview的内容会绑订在一起?
- windows 8 RT有开发蓝牙和API吗
- C++结构转C#的问题
或
SqlCommand b = new SqlCommand(sql);
b.Connection=con;你前面定义的sqlstring其实是conString
不要把数据库连接字符串和SQL语句搞混了最终查询的时候,必须同时把数据库连接字符串和SQL语句给DLL中的函数才行
你不给,它知道该去上哪个数据库里执行SQL吗
SqlCommand是个数据库执行对象
要使用SqlCommand,你得把它跟前面的SqlConnection关联起来才行
b.Connection=con;while (c.Read())之前需要判断c是否为null另外,建议你变量命名的时候,采用有意义的名称
比如SqlConnection可以命名为_Connection或者其他能够起到自注释功能的变量名称
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("错误");
}
}
}
}
}
或者把拼接好的SQL语句放出来看
http://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqlparameter(v=vs.110).aspx
b.Connection=a;
new SqlCommand("select * from [DL] where name = "+ textBox2.Text+"");
这是啥语法
b用的sql语句是前面的数据库连接字符串啊
而后面的sql语句,new完了没有给b啊
//登录页面设置
<table align="center" cellpadding="0" cellspacing="0" class="style1"
style="width: 325px">
<tr>
<td class="style2">
用 户 名:</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">
密 码:</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">
</td>
<td class="style4">
<asp:Button ID="Button1" runat="server" Text="登录" onclick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="重置" onclick="Button2_Click" />
</td>
<td class="style3">
</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();//关闭连接
}
}