use this
就是写个类 每次用时,new 一下就行了
public class ddd
ReadOnly Property DBConnection() As SqlConnection
Get
Dim SqlConn As SqlConnection
SqlConn = New SqlConnection(DBCONNECTIONSTRING)
SqlConn.Open()
Return SqlConn
End Get
End Propertyend class
就是写个类 每次用时,new 一下就行了
public class ddd
ReadOnly Property DBConnection() As SqlConnection
Get
Dim SqlConn As SqlConnection
SqlConn = New SqlConnection(DBCONNECTIONSTRING)
SqlConn.Open()
Return SqlConn
End Get
End Propertyend class
2,如果是大量的数据库操作,最好是用一个Connection对象,
连接也方便
public class cargoService : System.Web.Services.WebService
{
public cargoService()
{
//CODEGEN:该调用是 ASP.NET Web 服务设计器所必需的
InitializeComponent();
} #region Component Designer generated code
//Web 服务设计器所必需的
private IContainer components = null;
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
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";
// }
private static SqlConnection connCargo = new SqlConnection("server=192.168.1.10;DataBase=Contract;User ID=sa;Pwd=websa;Timeout=1200"); public static SqlConnection myConnCargo()
{
if(connCargo.State==ConnectionState.Closed)
{
connCargo.Open();
}
return connCargo;
}
public static DataSet GetUserMenuData()//这个方法也可以调用
{
string sql="select a.ParentId,b.* from UserMenu a left join SiteDirectory b on a.LinkId=b.LinkId where a.UserId='Default'";
SqlDataAdapter sda=new SqlDataAdapter(sql,cargoService.myConnCargo());
DataSet ds=new DataSet();
sda.Fill(ds);
sda.Dispose();
return ds;
}
=======================================================
asp.cs文件里的连接和调用private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if (!Page.IsPostBack )
{
DataSet ds=cargo.Service.cargoService.GetUserMenuData();//注意这儿和上面给的代码
this.ViewState["ds"]=ds;
DataView dv=ds.Tables[0].DefaultView;
dv.RowFilter="ParentId='0'";
...........
...........
public void BindGridToSource()
{
string sql="select * from SiteDirectory where SystemName='cargo' order by LinkId";
SqlDataAdapter sda=new SqlDataAdapter(sql,cargo.Service.cargoService.myConnCargo());//注意这儿.........
DataTable dt=new DataTable();
sda.Fill(dt);
sda.Dispose();
this.DataGrid1.DataSource=dt;
this.DataGrid1.DataBind();
}
}
}
现在说WEB CONFIG里面连接....
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ConnectionString" value="server=192.168.1.10;User Id=sa;PWD=websa;database=Contract" />
</appSettings>
<system.web> <!-- 动态调试编译
.............WEB SERVICE 里面写
public static string connString()
{
return System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
}aspx.cs文件里面写
for(int i=0;i<ds.Count;i++)
{ string polo=kf.Items[i].Cells[3].Text.ToString();
string strsql="select distinct cdesc from colour a,material_poh b,material_pod c where b.po_num = '"+polo+"'and c.material_poh_id = b.material_poh_id and a.color_code = c.color_code";
SqlDataAdapter sda = new SqlDataAdapter(strsql,cargo.Service.BaseDB.connString());//注意这儿...
DataTable tt=new DataTable();
sda.Fill(tt);