问题描述
stringtemp_username=tbusername.Text;stringtemp_pwd=tbpassword.Text;stringdb=Server.MapPath("mydb.mdb");stringConnectionStr="DataSource="+db+";Provider=Microsoft.Jet.OLEDB.4.0";OleDbConnectionmyConn=newOleDbConnection(ConnectionStr);myConn.Open();stringSql_str="selectPass_wordfromuserswhereUser_name='"+temp_username+"'";OleDbCommandcmd=newOleDbCommand(Sql_str,myConn);OleDbDataReadermyreader=cmd.ExecuteReader();if(myreader.Read()){Response.Write("<script>alert('用户已被注册');window.window.location.href='Default.aspx';</script>");}else{SqlCommandCm2=newSqlCommand("INSERTINTOuserlogin(temp_username,temp_pwd,email)VALUES('"+tbusername.Text+"','"+tbpassword.Text+"','"+email.Text+"')");inti=Cm2.ExecuteNonQuery();//message.InnerHtml="注册成功";Response.Write("<script>alert('注册成功');window.window.location.href='Default.aspx';</script>");}ExecuteNonQuery:Connection属性尚未初始化。行45:行46:tbpassword.Text+"','"+email.Text+"')");行47:inti=Cm2.ExecuteNonQuery();行48://message.InnerHtml="注册成功";行49:Response.Write("<script>alert('注册成功');window.window.location.href='Default.aspx';</script>");源文件:d:asp1zhuce.aspx.cs行:47堆栈跟踪:[InvalidOperationException:ExecuteNonQuery:Connection属性尚未初始化。]System.Data.SqlClient.SqlCommand.ValidateCommand(Stringmethod,Booleanasync)+291System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResultresult,StringmethodName,BooleansendToPipe)+72System.Data.SqlClient.SqlCommand.ExecuteNonQuery()+116_Default.Button1_Click(Objectsender,EventArgse)ind:asp1zhuce.aspx.cs:47System.Web.UI.WebControls.Button.OnClick(EventArgse)+75System.Web.UI.WebControls.Button.RaisePostBackEvent(StringeventArgument)+97System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(StringeventArgument)+7System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandlersourceControl,StringeventArgument)+11System.Web.UI.Page.RaisePostBackEvent(NameValueCollectionpostData)+33System.Web.UI.Page.ProcessRequestMain(BooleanincludeStagesBeforeAsyncPoint,BooleanincludeStagesAfterAsyncPoint)+4919请帮我改改,谢谢。
解决方案
解决方案二:
SqlCommandCm2=newSqlCommand("INSERTINTOuserlogin(temp_username,temp_pwd,email)VALUES('"+tbusername.Text+"','"+tbpassword.Text+"','"+email.Text+"')",myConn);
解决方案三:
或者Cm2.Connection=myConn;inti=Cm2.ExecuteNonQuery();
解决方案四:
为什么是access的要用SqlCommand呢.....不是OleDbCommand吗难道两个数据库?