小女子求教,实现一个数据库连接…

[复制链接]
查看11 | 回复6 | 2006-5-30 14:04:00 | 显示全部楼层 |阅读模式
想连接本地的数据库,查询并返回所有行的数据,本地的数据库Tab_flight,表是Tab_Flight,各位好心的大哥大姐能帮忙用vs.net做一个吗,不胜感激!


回复

使用道具 举报

千问 | 2006-5-30 14:04:00 | 显示全部楼层
using System.Collections;
using System;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;
namespace GetFlightInfo
{

///

/// Service1 的摘要说明。

///

public class GFlightInfo : System.Web.Services.WebService

{

private System.Data.SqlClient.SqlDataAdapter sqlAdapter;

public GFlightInfo()

{



//CODEGEN: 该调用是 ASP.NET Web 服务设计器所必需的

InitializeComponent();

SqlCommandBuilder builder;

string sql="select * from Tab_Flight";

string connstr="user id =sa;password=;database=Tab_Flight;server=localhost";

sqlAdapter=new SqlDataAdapter(sql,connstr);

builder=new SqlCommandBuilder(sqlAdapter);

}

#region 组件设计器生成的代码



//Web 服务设计器所必需的

private IContainer components = null;



///

/// 设计器支持所需的方法 - 不要使用代码编辑器修改

/// 此方法的内容。

///

private void InitializeComponent()

{

}

///

/// 清理所有正在使用的资源。

///

protected override void Dispose( bool disposing )

{

if(disposing && components != null)

{

components.Dispose();

}

base.Dispose(disposing);


}



#endregion

// WEB 服务示例

// HelloWorld() 示例服务返回字符串 Hello World

// 若要生成,请取消注释下列行,然后保存并生成项目

// 若要测试此 Web 服务,请按 F5 键

[WebMethod]

public DataSet GetFlightInfo()

{

DataSet ds=new DataSet();

sqlAdapter.Fill(ds,"Tab_Flight&quot

;

return ds;

}

}
}
这是我自己做的,可编译后返回的结果却是:
System.Data.SqlClient.SqlException: SQL Server 不存在或访问被拒绝。
at System.Data.SqlClient.ConnectionPool.GetConnection(Boolean& isInTransaction)
at System.Data.SqlClient.SqlConnectionPoolManager.GetPooledConnection(SqlConnectionString options, Boolean& isInTransaction)
at System.Data.SqlClient.SqlConnection.Open()
at System.Data.Common.DbDataAdapter.QuietOpen(IDbConnection connection, ConnectionState& originalState)
at System.Data.Common.DbDataAdapter.FillFromCommand(Object data, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, String srcTable)
at GetFlightInfo.GFlightInfo.GetFlightInfo() in c:\inetpub\wwwroot\getflightinfo\service1.asmx.cs:line 69
大虾们,能否指明一下哪里有问题了?
回复

使用道具 举报

千问 | 2006-5-30 14:04:00 | 显示全部楼层
Data Source=localhost;Initial catalog=Tab_flight;user id=sa;password=
连接字符串,不过你的表名和数据库名起同名不好的说
回复

使用道具 举报

千问 | 2006-5-30 14:04:00 | 显示全部楼层
我又重新写了一个:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;
namespace GetFlightInfo
{

///

/// Service1 的摘要说明。

///

[WebService(Namespace="http://www.mcp.org./WebSerices/Persistence&quot

]

public class Service1 : System.Web.Services.WebService

{

public Service1()

{

//CODEGEN: 该调用是 ASP.NET Web 服务设计器所必需的

InitializeComponent();

}

private System.Data.SqlClient.SqlConnection myCon;

private System.Data.SqlClient.SqlCommand sqlSelectCommand1;

private System.Data.SqlClient.SqlCommand sqlInsertCommand1;

private System.Data.SqlClient.SqlCommand sqlUpdateCommand1;

private System.Data.SqlClient.SqlCommand sqlDeleteCommand1;

private System.Data.SqlClient.SqlDataAdapter myAdapter;

#region 组件设计器生成的代码



//Web 服务设计器所必需的

private IContainer components = null;



///

/// 设计器支持所需的方法 - 不要使用代码编辑器修改

/// 此方法的内容。

///

private void InitializeComponent()

{

this.myCon = new System.Data.SqlClient.SqlConnection();

this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();

this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand();

this.sqlUpdateCommand1 = new System.Data.SqlClient.SqlCommand();

this.sqlDeleteCommand1 = new System.Data.SqlClient.SqlCommand();

this.myAdapter = new System.Data.SqlClient.SqlDataAdapter();

//

// myCon

//

this.myCon.ConnectionString = "workstation id=STARJET-309108F;packet size=4096;user id=dbo;integrated security=SS" +

"PI;data source=STARJET-309108F;persist security info=False;initial catalog=Tab_flight";



this.myCon.InfoMessage += new System.Data.SqlClient.SqlInfoMessageEventHandler(this.myCon_InfoMessage);

//

// myAdapter

//

this.myAdapter.DeleteCommand = this.sqlDeleteCommand1;

this.myAdapter.InsertCommand = this.sqlInsertCommand1;

this.myAdapter.SelectCommand = this.sqlSelectCommand1;

this.myAdapter.UpdateCommand = this.sqlUpdateCommand1;

}

///

/// 清理所有正在使用的资源。

///

protected override void Dispose( bool disposing )

{

if(disposing && components != null)

{

components.Dispose();

}

base.Dispose(disposing);


}



#endregion

// WEB 服务示例

// HelloWorld() 示例服务返回字符串 Hello World

// 若要生成,请取消注释下列行,然后保存并生成项目

// 若要测试此 Web 服务,请按 F5 键
//
[WebMethod]
//
public string HelloWorld()
//
{
//
return "Hello World";
//
}


[WebMethod]

public string GetAll()

{

string comstr = string.Empty;

DataSet myDs = new DataSet();

try

{


myCon.Open();

System.Data.SqlClient.SqlCommandBuilder myBuilder = new SqlCommandBuilder(myAdapter);

comstr = "SELECT * FROM Tab_Flight";

this.myAdapter.SelectCommand.Connection = this.myCon;


this.myAdapter.SelectCommand.CommandText = comstr;


myAdapter.Fill(myDs,"flight&quot

;



}

catch(Exception ex)

{

comstr = ex.Message;

}

this.myCon.Close();

return comstr;


}

private void myCon_InfoMessage(object sender, System.Data.SqlClient.SqlInfoMessageEventArgs e)

{



}


}
}
原代码返回的dataset没有数据记录,我就改成返回异常
这是测试的,显示登录不了。
怎么回事,大虾们,帮帮忙啊。
回复

使用道具 举报

千问 | 2006-5-30 14:04:00 | 显示全部楼层
这论坛实在不行,看的人多,真正回贴帮助的人太少。。
回复

使用道具 举报

千问 | 2006-5-30 14:04:00 | 显示全部楼层
看不懂
回复

使用道具 举报

千问 | 2006-5-30 14:04:00 | 显示全部楼层
3楼你返回的comstr是一条SQL语句而不是DataSet,所以没有记录集。你应该改成return myDS
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行