首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

怎样在.NET中执行SQL语句,并把结果查返回显示在WEB面面中(不知哪错了,新人求指教),该如何处理

2012-02-17 
怎样在.NET中执行SQL语句,并把结果查返回显示在WEB面面中(不知哪错了,新人求指教)备注:假设已经有一个名为

怎样在.NET中执行SQL语句,并把结果查返回显示在WEB面面中(不知哪错了,新人求指教)
备注:假设已经有一个名为sqlConnection1的SqlConnection  

using   System;
using   System.Collections;
using   System.ComponentModel;
using   System.Data.SqlClient;
using   System.Drawing;
using   System.Web;
using   System.Web.SessionState;
using   System.Web.UI;
using   System.Web.UI.WebControls;
using   System.Web.UI.HtmlControls;

namespace   WebApplication6
{
///   <summary>
///   WebForm1   的摘要说明。
///   </summary>
public   class   WebForm1   :   System.Web.UI.Page
{
protected   System.Web.UI.WebControls.Label   Label1;
protected   System.Data.SqlClient.SqlConnection   sqlConnection1;
protected   System.Data.SqlClient.SqlCommand   sqlCommand1;
protected   System.Web.UI.WebControls.Button   Button1;

private   void   Page_Load(object   sender,   System.EventArgs   e)
{
//   在此处放置用户代码以初始化页面
}

#region   Web   窗体设计器生成的代码
override   protected   void   OnInit(EventArgs   e)
{
//
//   CODEGEN:   该调用是   ASP.NET   Web   窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

///   <summary>
///   设计器支持所需的方法   -   不要使用代码编辑器修改
///   此方法的内容。
///   </summary>
private   void   InitializeComponent()
{        
this.sqlConnection1   =   new   System.Data.SqlClient.SqlConnection();
this.sqlCommand1   =   new   System.Data.SqlClient.SqlCommand();
this.Button1.Click   +=   new   System.EventHandler(this.Button1_Click);
//  
//   sqlConnection1
//  
this.sqlConnection1.ConnectionString   =   "workstation   id=ANT;packet   size=4096;integrated   security=SSPI;data   source=ANT;pers "   +
"ist   security   info=False;initial   catalog=anttest ";
//  
//   sqlCommand1
//  
this.sqlCommand1.Connection   =   this.sqlConnection1;
this.Load   +=   new   System.EventHandler(this.Page_Load);

}
#endregion

private   void   Button1_Click(object   sender,   System.EventArgs   e)
{}
public   class   SQLDR

{

static   void   Main(string[]   args)

{

//   Step   1:   Make   a   connection.

SqlConnection   sqlConnection1   =   new   sqlConnection();

                SqlConnection1.ConnectionString   =   "Provider=SqlClient.1; "   +

"Integrated   Security=SSPI; "   + "Persist   Security   Info=False; "   +

"Initial   Catalog=Cars; "   + "Data   Source=BIGMANU; ";

sqlConnection1.Open();

//   Step   2:   Create   a   SQL   command.

string   strSQL   =   "SELECT   *   FROM   students   WHERE   sex= '女 ' ";

SqlCommand   sqlCommand1   =   new   SqlCommand(strSQL,   sqlConnection1);

//   Step   3:   Obtain   a   data   reader   ala   ExecuteReader().



SqlDataReader   myDataReader;

SqlDataReader   =   SqlCommand.ExecuteReader();

  //   Step   4:   Loop   over   the   results.
while   (myDataReader.Read())
{

Console.WriteLine( "Red   car:   "   +

myDataReader[ "strSQL "].ToString());

}

  myDataReader.Close();

sqlConnection1.Close();

}

}



[解决办法]
Console.WriteLine( "Red car: " + myDataReader[ "strSQL "].ToString());
====================================================================
Response.Write( "Red car: " + myDataReader[ "strSQL "].ToString() + " <br /> ");

在网页里用控制台的Console.WriteLine,打算给谁看啊


[解决办法]
楼上那位大哥已经指出症结了,还有你的这些程序应该放在Page_load中?
[解决办法]
static void Main(string[] args)
==============================
居然还有这个..

private void Page_Load(object sender, EventArgs e)
{

}

热点排行