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

love1907 2007-07-17 11:44:08
备注:假设已经有一个名为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();

}

}

...全文
220 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
amandag 2007-07-18
  • 打赏
  • 举报
回复
static void Main(string[] args)
==============================
居然还有这个..

private void Page_Load(object sender, EventArgs e)
{

}
ruan_hg 2007-07-18
  • 打赏
  • 举报
回复
楼上那位大哥已经指出症结了,还有你的这些程序应该放在Page_load中?
amandag 2007-07-17
  • 打赏
  • 举报
回复
Console.WriteLine("Red car: " + myDataReader["strSQL"].ToString());
====================================================================
Response.Write("Red car: " + myDataReader["strSQL"].ToString() + "<br />");

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

111,115

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧