zhuyc - 2008-1-17 11:10:00
我是个新手,在用。NET做一个简单的网页,连接了一下数据库,在C#里以前一直只是把数据库拉到页面就能连接了,但是这次就不行了,出现登陆失败的错误!
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace WebApplication3
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Data.SqlClient.SqlCommand sqlSelectCommand1;
protected System.Data.SqlClient.SqlCommand sqlInsertCommand1;
protected System.Data.SqlClient.SqlConnection sqlConnection1;
protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
private void Page_Load(object sender, System.EventArgs e)
{
string sql = "SELECT * FROM user_info";
string aaa ;
sqlConnection1.Open();
sqlSelectCommand1 = new SqlCommand(sql,sqlConnection1);
SqlDataReader dr = sqlSelectCommand1.ExecuteReader();
if(dr.Read())
aaa = "ok";
else
aaa = "no";
string html = "<table><tr><td>"+aaa+"</td></tr></table>";
Response.Write(html);
// 在此处放置用户代码以初始化页面
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
//
// sqlSelectCommand1
//
this.sqlSelectCommand1.CommandText = "SELECT telephone, input_time, bill_time, quit_time FROM user_info";
this.sqlSelectCommand1.Connection = this.sqlConnection1;
//
// sqlInsertCommand1
//
this.sqlInsertCommand1.CommandText = "INSERT INTO user_info(telephone, input_time, bill_time, quit_time) VALUES (@telep" +
"hone, @input_time, @bill_time, @quit_time); SELECT telephone, input_time, bill_t" +
"ime, quit_time FROM user_info";
this.sqlInsertCommand1.Connection = this.sqlConnection1;
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@telephone", System.Data.SqlDbType.VarChar, 12, "telephone"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@input_time", System.Data.SqlDbType.VarChar, 50, "input_time"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@bill_time", System.Data.SqlDbType.VarChar, 50, "bill_time"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@quit_time", System.Data.SqlDbType.VarChar, 50, "quit_time"));
//
// sqlConnection1
//
this.sqlConnection1.C +
"ource=zhu;persist security info=False;initial catalog=TEL_XYELY";
//
// sqlDataAdapter1
//
this.sqlDataAdapter1.InsertCommand = this.sqlInsertCommand1;
this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
new System.Data.Common.DataTableMapping("Table", "user_info", new System.Data.Common.DataColumnMapping[] {
new System.Data.Common.DataColumnMapping("telephone", "telephone"),
new System.Data.Common.DataColumnMapping("input_time", "input_time"),
new System.Data.Common.DataColumnMapping("bill_time", "bill_time"),
new System.Data.Common.DataColumnMapping("quit_time", "quit_time")})});
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
付上代码请高手指正!
houtinghua - 2008-1-22 10:42:00
尽量不要用.net自己的数据源
page_load的代码
SqlConnection conn = new SqlConnection( "你的数据库连接字符串" );//Password=sa;User ID=sa;Initial Catalog=dnt2;Data Source=.
conn.Open();
Command com = new Command( conn,"SELECT * FROM user_info" );
SqlDataReader sdr = com.ExecuteReader();
if(dr.Read())
aaa = "ok";
else
aaa = "no";
string html = "<table><tr><td>"+aaa+"</td></tr></table>";
conn.Close()
Response.Write(html);