我是初学者,执行web连接数据库好,进行查找时候出现问题,大家帮忙看看。CSQL类代码using System;
using System.Data ;
using System.Data.SqlClient ;namespace WebApplicationWeb
{
/// <summary>
/// CSQL 的摘要说明。
///
/// </summary>
public class CSQL
{
public static string SConnStr;
protected SqlConnection myConnection;
protected SqlCommand myCommand;
protected SqlDataReader myDataReader;
public CSQL(string SDataSource,string SCatalog)
{
SConnStr="Data Source="+SDataSource+";Integrated Security=SSPI;Initial Catalog="+SCatalog;
} public void Connection()
{
myConnection = new SqlConnection (SConnStr);
}
public void Search(ref string SSql)
{
myConnection.Open ();
myCommand = new SqlCommand (SSql,myConnection);
}
~CSQL()
{
myConnection.Close ();
} }
public class Ccheck:CSQL
{
public Ccheck(string SDataSource,string SCatalog):base(SDataSource,SCatalog)
{
;
}
public bool Check()
{
myDataReader=myCommand.ExecuteReader();//<<<<<<出错<<<<<<<
if(!myDataReader.Read())//如果没有读到记录
return false;
else
return true;
} }
}Web页实现登录验证。
private void LinkButton1_Click(object sender, System.EventArgs e)
{
string Ssql;
Ccheck CmyCheck=new Ccheck("localhost","user");
CmyCheck.Connection ();
Ssql="select * from user where username='"+username.Text+"' and password='"+password.Text +"'";
CmyCheck.Search(ref Ssql);
if (CmyCheck.Check())
Response.Redirect ("WebForm4.aspx");//验证通过跳转
else
Response.Write ("密码或用户名错误"); }编译通过,当按下按钮时出现错误。在关键字 'user' 附近有语法错误。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Data.SqlClient.SqlException: 在关键字 'user' 附近有语法错误。源错误:
行 45: public bool Check()
行 46: {
行 47: myDataReader=myCommand.ExecuteReader();
行 48: if(!myDataReader.Read())//如果没有读到记录
行 49: return false;
源文件: c:\inetpub\wwwroot\webapplicationweb\csql.cs 行: 47 堆栈跟踪:
[SqlException: 在关键字 'user' 附近有语法错误。]
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream)
System.Data.SqlClient.SqlCommand.ExecuteReader()
WebApplicationWeb.Ccheck.Check() in c:\inetpub\wwwroot\webapplicationweb\csql.cs:47
WebApplicationWeb.WebForm1.LinkButton1_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\webapplicationweb\webform1.aspx.cs:72
System.Web.UI.WebControls.LinkButton.OnClick(EventArgs e)
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain()
using System.Data ;
using System.Data.SqlClient ;namespace WebApplicationWeb
{
/// <summary>
/// CSQL 的摘要说明。
///
/// </summary>
public class CSQL
{
public static string SConnStr;
protected SqlConnection myConnection;
protected SqlCommand myCommand;
protected SqlDataReader myDataReader;
public CSQL(string SDataSource,string SCatalog)
{
SConnStr="Data Source="+SDataSource+";Integrated Security=SSPI;Initial Catalog="+SCatalog;
} public void Connection()
{
myConnection = new SqlConnection (SConnStr);
}
public void Search(ref string SSql)
{
myConnection.Open ();
myCommand = new SqlCommand (SSql,myConnection);
}
~CSQL()
{
myConnection.Close ();
} }
public class Ccheck:CSQL
{
public Ccheck(string SDataSource,string SCatalog):base(SDataSource,SCatalog)
{
;
}
public bool Check()
{
myDataReader=myCommand.ExecuteReader();//<<<<<<出错<<<<<<<
if(!myDataReader.Read())//如果没有读到记录
return false;
else
return true;
} }
}Web页实现登录验证。
private void LinkButton1_Click(object sender, System.EventArgs e)
{
string Ssql;
Ccheck CmyCheck=new Ccheck("localhost","user");
CmyCheck.Connection ();
Ssql="select * from user where username='"+username.Text+"' and password='"+password.Text +"'";
CmyCheck.Search(ref Ssql);
if (CmyCheck.Check())
Response.Redirect ("WebForm4.aspx");//验证通过跳转
else
Response.Write ("密码或用户名错误"); }编译通过,当按下按钮时出现错误。在关键字 'user' 附近有语法错误。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Data.SqlClient.SqlException: 在关键字 'user' 附近有语法错误。源错误:
行 45: public bool Check()
行 46: {
行 47: myDataReader=myCommand.ExecuteReader();
行 48: if(!myDataReader.Read())//如果没有读到记录
行 49: return false;
源文件: c:\inetpub\wwwroot\webapplicationweb\csql.cs 行: 47 堆栈跟踪:
[SqlException: 在关键字 'user' 附近有语法错误。]
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream)
System.Data.SqlClient.SqlCommand.ExecuteReader()
WebApplicationWeb.Ccheck.Check() in c:\inetpub\wwwroot\webapplicationweb\csql.cs:47
WebApplicationWeb.WebForm1.LinkButton1_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\webapplicationweb\webform1.aspx.cs:72
System.Web.UI.WebControls.LinkButton.OnClick(EventArgs e)
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain()
解决方案 »
- repeater循环累加
- 出力的csv文件内容win2k和vista不一致!!!!!高分悬赏
- asp的网页,导航栏问题
- 如何读取Repeater和绑定字段数据
- 加载控件为什么有时候不成功 ,控件没有移动到tableCell中去
- 子报表错误:Invalid document structure
- 请教:编译器错误信息: 编译器失败,错误代码为 128。解决给100分。急!!
- WebClient 远程服务器返回错误: (400) 错误的请求。 直接URL输入就可以,而写代码请求就这样
- A标签事件怎么不能调用
- 如何实现打灰色小勾的CheckBox?就像在Windows下同时查看多个属性(存档、只读等等)不同的文件的属性时显示checkbox的一样.
- .net中如何调用模板并且运行?
- SqlDataSource中...where后的列名和值到用Session.如何解决??
user name time data 等