app-code 里面的内容://验证数据库
public static bool isused(string accmd)
{
OleDbConnection oledbconn = myconn();
oledbconn.Open();
OleDbCommand mycomm = new OleDbCommand(accmd, oledbconn);
int i = Convert.ToInt32(mycomm.ExecuteReader());
oledbconn.Close();
return i > 0 ? true : false;
}点击按钮的内容:protected void Button1_Click(object sender, EventArgs e)
{
string accmd = "select 用户名 from[用户信息表]where 1=1='" + this.TextBox1.Text + "'";
if (dbconn.isused(accmd))
{
Response.Write("<script language='javascript'>window.alert('次ID已经被注册不可用')</script>");
}
}
问题是:
无法将类型为“System.Data.OleDb.OleDbDataReader”的对象强制转换为类型“System.IConvertible”。 if (dbconn.isused(accmd)) 这行 是红色的 大家帮帮忙 谢谢 我是 大菜鸟
public static bool isused(string accmd)
{
OleDbConnection oledbconn = myconn();
oledbconn.Open();
OleDbCommand mycomm = new OleDbCommand(accmd, oledbconn);
int i = Convert.ToInt32(mycomm.ExecuteReader());
oledbconn.Close();
return i > 0 ? true : false;
}点击按钮的内容:protected void Button1_Click(object sender, EventArgs e)
{
string accmd = "select 用户名 from[用户信息表]where 1=1='" + this.TextBox1.Text + "'";
if (dbconn.isused(accmd))
{
Response.Write("<script language='javascript'>window.alert('次ID已经被注册不可用')</script>");
}
}
问题是:
无法将类型为“System.Data.OleDb.OleDbDataReader”的对象强制转换为类型“System.IConvertible”。 if (dbconn.isused(accmd)) 这行 是红色的 大家帮帮忙 谢谢 我是 大菜鸟
用这个方法
这个得到结果是影响的行数
如果影响的行数是0就是不存在
如果影响的行数大于0就存在
<head runat="server">
<title>无标题页</title>
<script type="text/javascript">
var xmlhttp;
function Validation()
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
var name=document.getElementById("UserName");
xmlhttp.open("post","AjaxTest.aspx?userName="+name.value);
xmlhttp.onreadystatechange=OnMessageback;
xmlhttp.send(null);
}
function OnMessageback()
{
if(xmlhttp.readystate==4&&xmlhttp.status==200)
{
document.write(xmlhttp.responsetext);
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="text" id="UserName" onchange="Validation()" />
</div>
</form>
</body>
</html>using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;public partial class ajax_AjaxTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string userName = Request.QueryString["userName"];
if (!string.IsNullOrEmpty(userName))
{
string connString = "server=.;database=GameCardSale;user id=sa;password=123456";
SqlConnection conn=new SqlConnection(connString);
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select count(*) from UserInfo where userId=@userId";
cmd.Parameters.Add("@userId", SqlDbType.NVarChar, 50).Value = userName;
conn.Open();
int count = (int)cmd.ExecuteScalar();
conn.Close();
if (count>0)
{
Response.Write("用户名已存在!");
}
else
{
Response.Write("恭喜你,此用户名可以使用!");
}
}
}
}
<script type="text/javascript">
var xmlHttp;
function createXMLHttpRequest()
{
if(window.ActiveXObject)
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
xmlHttp = new XMLHttpRequest();
}
}
function CheckUserName()
{
var us=document.getElementById("txtname").value;
if(us!="")
{
createXMLHttpRequest();
var url= "RegistValidate.ashx?username="+escape(document.getElementById("txtname").value);
xmlHttp.open("GET",url,true);
xmlHttp.onreadystatechange=ShowResult;
xmlHttp.send(null);
}
}
function ShowResult()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
var s;
s=xmlHttp.responseText;
alert(s);
}
}
}
</script> string sql = "select count(*) from [user] where username = '"+username.Text+"'";
int count = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.Conn, CommandType.Text, sql));
if (count > 0)
flag = true;'存在
else
flag = false;'不存在
{
string accmd = "select 用户名 from 用户信息表 where 用户名='" + this.TextBox1.Text + "'";
if (dbconn.ExcuteSql(accmd))
{
Response.Write("<script language='javascript'>window.alert('次ID已经被注册不可用');</script>");
}
}
你应该用ExecuteScalar()方法,误用了ExecuteReader,换一下就可以了