下面的代码怎么包错啊?可以编译过去,但是用的时候包错:
SqlConnection cn=new SqlConnection("server=.;database=stu;user id=sa;password=");
//打开数据库
cn.Open();
string user=tbName.Text;
string pwd=password.Text;
//sql语句
string sql="select * from users where userName='user' and password='pwd'";
//发送sql语句
SqlCommand cmd=new SqlCommand(sql,cn);
//执行sql语句
int a=cmd.ExecuteNonQuery();
MessageBox.Show(a.ToString());
SqlConnection cn=new SqlConnection("server=.;database=stu;user id=sa;password=");
//打开数据库
cn.Open();
string user=tbName.Text;
string pwd=password.Text;
//sql语句
string sql="select * from users where userName='user' and password='pwd'";
//发送sql语句
SqlCommand cmd=new SqlCommand(sql,cn);
//执行sql语句
int a=cmd.ExecuteNonQuery();
MessageBox.Show(a.ToString());
请参阅此消息的结尾。************** 异常文本 **************
System.Data.SqlClient.SqlException: 不能比较或排序 text、ntext 和 image 数据类型,除非使用 IS NULL 或 LIKE 运算符。
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at WindowsApplication1.Form1.button1_Click(Object sender, EventArgs e) in c:\documents and settings\administrator\桌面\windowsapplication1\form1.cs:line 217
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
************** 已加载的程序集 **************
mscorlib
程序集版本: 1.0.5000.0
Win32 版本: 1.1.4322.573
基本代码: file:///c:/windows/microsoft.net/framework/v1.1.4322/mscorlib.dll
----------------------------------------
WindowsApplication1
程序集版本: 1.0.2764.38291
Win32 版本: 1.0.2764.38291
基本代码: file:///C:/Documents%20and%20Settings/Administrator/桌面/WindowsApplication1/bin/Debug/WindowsApplication1.exe
----------------------------------------
System.Windows.Forms
程序集版本: 1.0.5000.0
Win32 版本: 1.1.4322.573
基本代码: file:///c:/windows/assembly/gac/system.windows.forms/1.0.5000.0__b77a5c561934e089/system.windows.forms.dll
----------------------------------------
System
程序集版本: 1.0.5000.0
Win32 版本: 1.1.4322.573
基本代码: file:///c:/windows/assembly/gac/system/1.0.5000.0__b77a5c561934e089/system.dll
----------------------------------------
System.Drawing
程序集版本: 1.0.5000.0
Win32 版本: 1.1.4322.573
基本代码: file:///c:/windows/assembly/gac/system.drawing/1.0.5000.0__b03f5f7f11d50a3a/system.drawing.dll
----------------------------------------
System.Xml
程序集版本: 1.0.5000.0
Win32 版本: 1.1.4322.573
基本代码: file:///c:/windows/assembly/gac/system.xml/1.0.5000.0__b77a5c561934e089/system.xml.dll
----------------------------------------
System.Data
程序集版本: 1.0.5000.0
Win32 版本: 1.1.4322.573
基本代码: file:///c:/windows/assembly/gac/system.data/1.0.5000.0__b77a5c561934e089/system.data.dll
----------------------------------------
System.EnterpriseServices
程序集版本: 1.0.5000.0
Win32 版本: 1.1.4322.573
基本代码: file:///c:/windows/assembly/gac/system.enterpriseservices/1.0.5000.0__b03f5f7f11d50a3a/system.enterpriseservices.dll
----------------------------------------
mscorlib.resources
程序集版本: 1.0.5000.0
Win32 版本: 1.1.4322.573
基本代码: file:///c:/windows/assembly/gac/mscorlib.resources/1.0.5000.0_zh-chs_b77a5c561934e089/mscorlib.resources.dll
----------------------------------------
System.Windows.Forms.resources
程序集版本: 1.0.5000.0
Win32 版本: 1.1.4322.573
基本代码: file:///c:/windows/assembly/gac/system.windows.forms.resources/1.0.5000.0_zh-chs_b77a5c561934e089/system.windows.forms.resources.dll
----------------------------------------************** JIT 调试 **************
计算机的配置文件(machine.config)的
system.windows.forms 节中必须设置 jitDebugging 值。
编译应用程序时还必须启用\r\n调试。\r\n\r\n例如: \r\n\r\n<configuration>\r\n <system.windows.forms jitDebugging="true" />\r\n</configuration>\r\n\r\n启用 JIT 调试后,任何未处理的异常\r\n都将被发送到此计算机上注册的 JIT 调试器,\r\n而不是由此对话框处理。\r\n
string sql="select * from users where userName='"+user+"' and password='"+pwd+"'";
string sql = string.Fomat("select * from users where userName={0} and password = {1}",user,pwd);
userName char
password text
============================================
LZ,你是想拿窗体中的值还是自己些下的值
拿窗体必须先定义,后使用:
("server=.;database=stu; uid='"+user+"';pwd='"+pwd+"'")
自定义的是:
("server=.;database=stu; uid=sa;pwd=sa")
这个不是连接sqlserver数据库吗?直接把用户名和密码写进去不就行了吗?还需要改什么呢?
userId 整型 自动增长列
userName char
password text
admin列在哪?
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;namespace WindowsApplication1
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox password;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox conf;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox tbName;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.button2 = new System.Windows.Forms.Button();
this.label3 = new System.Windows.Forms.Label();
this.conf = new System.Windows.Forms.TextBox();
this.password = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.tbName = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.button2);
this.groupBox1.Controls.Add(this.label3);
this.groupBox1.Controls.Add(this.conf);
this.groupBox1.Controls.Add(this.password);
this.groupBox1.Controls.Add(this.label2);
this.groupBox1.Controls.Add(this.button1);
this.groupBox1.Controls.Add(this.tbName);
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Location = new System.Drawing.Point(24, 24);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(368, 200);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "用户注册信息";
//
// button2
//
this.button2.Location = new System.Drawing.Point(184, 120);
this.button2.Name = "button2";
this.button2.TabIndex = 5;
this.button2.Text = "取消";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// label3
//
this.label3.Location = new System.Drawing.Point(8, 80);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(104, 23);
this.label3.TabIndex = 6;
this.label3.Text = "请输入确认密码:";
//
// conf
//
this.conf.Location = new System.Drawing.Point(120, 72);
this.conf.Name = "conf";
this.conf.Size = new System.Drawing.Size(144, 21);
this.conf.TabIndex = 3;
this.conf.Text = "";
this.conf.TextChanged += new System.EventHandler(this.textBox1_TextChanged_1);
//
// password
//
this.password.Location = new System.Drawing.Point(120, 48);
this.password.Name = "password";
this.password.PasswordChar = '*';
this.password.Size = new System.Drawing.Size(144, 21);
this.password.TabIndex = 2;
this.password.Text = "";
//
// label2
//
this.label2.Location = new System.Drawing.Point(8, 56);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(104, 23);
this.label2.TabIndex = 3;
this.label2.Text = "请输入您的密码:";
//
// button1
//
this.button1.Location = new System.Drawing.Point(40, 120);
this.button1.Name = "button1";
this.button1.TabIndex = 4;
this.button1.Text = "提交";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// tbName
//
this.tbName.Location = new System.Drawing.Point(120, 24);
this.tbName.Name = "tbName";
this.tbName.Size = new System.Drawing.Size(144, 21);
this.tbName.TabIndex = 1;
this.tbName.Text = "";
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(100, 16);
this.label1.TabIndex = 0;
this.label1.Text = "请输入用户名:";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(432, 286);
this.Controls.Add(this.groupBox1);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Name = "Form1";
this.Text = "恩施斯瑞程序演示";
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void button1_Click(object sender, System.EventArgs e)
{
if(tbName.Text=="")
{
string show;
show=MessageBox.Show("您还没有输入用户名","请输入用户名:",MessageBoxButtons.AbortRetryIgnore).ToString();
//根据用户点击不同的按钮,来对行为进行不同的判断
switch(show){
case "Abort":
Form1.ActiveForm.Close();
break;
case "Retry":
tbName.Focus();
break;
case "Ignore":
password.Focus();
break;
}
}
else
{
//MessageBox(userName.Text,);
//MessageBox.Show("您输入的用户名是:"+tbName.Text+"\n您输入的密码是:"+password.Text+"","您输入的用户名是:",MessageBoxButtons.OK);
//实力化连接数据库字符串
SqlConnection cn=new SqlConnection("server=.;database=stu;user id=sa;password=");
//打开数据库
cn.Open();
string user=tbName.Text;
string pwd=password.Text;
//sql语句
string sql = string.Format("select * from users where userName={0} and [password]={1}",user,pwd);
MessageBox.Show(sql);
//发送sql语句
SqlCommand cmd=new SqlCommand(sql,cn);
//执行sql语句
int a=cmd.ExecuteNonQuery();
MessageBox.Show(a.ToString());
}
} private void textBox1_TextChanged(object sender, System.EventArgs e)
{
} private void textBox1_TextChanged_1(object sender, System.EventArgs e)
{
} private void button2_Click(object sender, System.EventArgs e)
{
tbName.Text="";
password.Text="";
conf.Text="";
}
}
}
cn.Open();
既然在连接的时候没有报错,连接已经成功了,应该是命令的问题.你的表结构改成了varchar()没有?
把sql也输出下?
加单引号就可以了啊?但是怎么老是弹出-1啊?那怎么判断用户和密码是正确的呢?
用SqlDataReader read = cmd.ExcutReader()
if(read.Read())
{
MessageBox.Show("登录成功!");
}
else
{MessageBox.Show("失败!");
}
int a=cmd.ExecuteNonQuery();
在把OBJ转换成INT类型!!
偶是小菜,说错了大家别笑我~~