📄 dbservice.asmx.cs
字号:
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;
using System.Xml;
using System.IO;
using System.Reflection;
namespace WSSQLsrv2000
{
#region "Description for ConnectDB"
//Author : Rasheed
//Created Date : 18 Feb 2005
//This Class used to provide interface to other applications to consume.
//This webservice helps to work around with SQL server 2000
//Note: Author is not responsible for any problem cause in your system while using this code
#endregion
[WebService(Namespace="http://192.168.52.173")] //IIS machine ip address
public class ConnectDB : System.Web.Services.WebService
{
CConnection oCon = new CConnection(); //This class provides database services
private CConnection.LoginInfo oLoginInfo; //Structure object to maintain Login information
public ConnectDB()
{
//CODEGEN: This call is required by the ASP.NET Web Services Designer
InitializeComponent();
}
#region Component Designer generated code
//Required by the Web Services Designer
private IContainer components = null;
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if(disposing && components != null)
{
components.Dispose();
}
base.Dispose(disposing);
}
#endregion
[WebMethod] public bool fnConnect(string sDataSource, string sUserid, string sPassword)
{
//To check the connection status with given details
oLoginInfo.sServer = sDataSource;
oLoginInfo.sLoginName = sUserid;
oLoginInfo.sPassword = sPassword;
oLoginInfo.sDatabase = "";
if (oCon.fnConnectStatus(oLoginInfo))
{
//Save the details if the connection status is true for further manupulation
SetDBConfigSettings(sDataSource,sUserid,sPassword);
return true;
}
else
{
//Connection failed in various reasons
return false;
}
}
[WebMethod] public DataSet DBList()
{
//To list the available database in particular server
//To achive this do
//1.Get connection details from XML file
//2.Connect with Server
//3.Execute and return value as Dataset
GetDBConfigSettings();
bool bconState = oCon.fnConnectStatus(oLoginInfo);
SqlCommand dbCMD = new SqlCommand();
dbCMD = oCon.ReturnCommand("sp_databases");
dbCMD.CommandType = CommandType.StoredProcedure;
SqlDataAdapter DA = new SqlDataAdapter();
DA.SelectCommand = dbCMD;
DataSet dsDBList = new DataSet();
DA.Fill(dsDBList, "Databases");
return dsDBList;
}
[WebMethod] public DataSet ReturnResult(string sSql)
{
//Excute query and returns result as Dataset
DataSet ds = new DataSet();
GetDBConfigSettings();
bool bconState = oCon.fnConnectStatus(oLoginInfo);
ds = oCon.ReturnDataset(sSql);
return ds;
}
private bool GetDBConfigSettings()
{
//Read the Connection details from the XML file and load it in to the
//local variables
XmlDocument xmlDoc = new XmlDocument();
string strAppWorkingDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
//Get the configuration data from the config XML file
//strAppWorkingDir = strAppWorkingDir.Substring(6,strAppWorkingDir.Length - 6);
XmlTextReader ConfigReader = new XmlTextReader(strAppWorkingDir + @"\Config.xml");
xmlDoc.Load(ConfigReader);
//
//Cleanup - Close I/O
ConfigReader.Close();
//Assign the values into the local variables
oLoginInfo.sServer = xmlDoc.SelectSingleNode("Configuration//DatabaseServer").InnerText;
oLoginInfo.sLoginName = xmlDoc.SelectSingleNode("Configuration//DatabaseLogin").InnerText;
oLoginInfo.sPassword = xmlDoc.SelectSingleNode("Configuration//DatabasePassword").InnerText;
oLoginInfo.sDatabase = xmlDoc.SelectSingleNode("Configuration//DatabaseName").InnerText;
return true;
}
[WebMethod]public bool SetDBConfigSettings(string sDataSource, string sUserid, string sPassword)
{
//Write the connections details into the local XML file
XmlDocument xmlDoc = new XmlDocument();
string strAppWorkingDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
//Get the configuration data from the config XML file
strAppWorkingDir = strAppWorkingDir.Substring(6,strAppWorkingDir.Length - 6);
XmlTextReader ConfigReader = new XmlTextReader(strAppWorkingDir + @"\Config.xml");
xmlDoc.Load(ConfigReader);
xmlDoc.SelectSingleNode("Configuration//DatabaseServer").InnerText = sDataSource;
xmlDoc.SelectSingleNode("Configuration//DatabaseLogin").InnerText = sUserid;
xmlDoc.SelectSingleNode("Configuration//DatabasePassword").InnerText = sPassword;
xmlDoc.SelectSingleNode("Configuration//DatabaseName").InnerText = "";
//Cleanup - Close I/O
ConfigReader.Close();
xmlDoc.PreserveWhitespace = true;
//Over write the existing File
xmlDoc.Save(strAppWorkingDir + @"\Config.xml");
return true;
}
[WebMethod]public bool SetDBName(string sDB)
{
//Update Database name into the XML file for further execution
XmlDocument xmlDoc = new XmlDocument();
string strAppWorkingDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
//Get the configuration data from the config XML file
strAppWorkingDir = strAppWorkingDir.Substring(6,strAppWorkingDir.Length - 6);
XmlTextReader ConfigReader = new XmlTextReader(strAppWorkingDir + @"\Config.xml");
xmlDoc.Load(ConfigReader);
xmlDoc.SelectSingleNode("Configuration//DatabaseName").InnerText = sDB;
//Cleanup - Close I/O
ConfigReader.Close();
xmlDoc.PreserveWhitespace = true;
//Over write the existing File
xmlDoc.Save(strAppWorkingDir + @"\Config.xml");
return true;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -