给mail,发给你
[email protected]
[email protected]
解决方案 »
- DataGridView合并行奇怪的问题
- C# EXECL问题?
- guid类型填充到datagridview中的奇怪问题
- 相对路径的问题~~~
- 我把IsMdiContainer设为True后,这个skinEngine控件就没法用了,有什么办法吗?
- 在C#中怎么从FTP上下载文件,和上传文件到FTP上去。
- ASP.NET Forums 的源代码打开了老是控件创建错误,为什么??
- 请问在文本框中怎么能控制输入数字--在线等待
- DataSet或DataView中如何实现类似数据库命令 LIKE 的筛选功能?
- 关于VS.NET的测试工具
- 如何解决在代码里面包含“//”字符得问题
- C#中如何制定任务计划?
真是谢谢你了
[email protected]
private SQLDMO.NameList sqlServers = null; sqlServers = sqlApp.ListAvailableSQLServers();
for(int i=0;i<sqlServers.Count;i++)
{
object srv = sqlServers.Item( i + 1);
if(srv != null)
{
this.comboBoxServer.Items.Add(srv);
}
}
添加对SQLDMO的引用
谢谢!这个问题我文了好久了!
大家帮忙查一下!
就报错了,不知道为什么
using System.Data.SqlClient;
using System.Data;
using System.Data.Common;
using System.Windows.Forms;
using System.Collections;namespace CustomerLodgingDemo
{
/// <summary>
///
/// </summary>
public class DataControl
{
private SqlConnection sqlconn;
private SqlCommand sqlcomm;
public DataControl()
{
//
// TODO: 在此处添加构造函数逻辑
//
sqlconn=new SqlConnection();
sqlconn.ConnectionString="data source=TORIO005;initial catalog=restaurant;password=hotel;persist security info=True;user id=hotel;workstation id=ALUCARD;packet size=4096";
sqlcomm=new SqlCommand();
sqlcomm.Connection=sqlconn;
} public bool ConnectionTest(string datasource,string account,string password)
{
//测试数据库连接是否正常
SqlConnection newconn=new SqlConnection("data source="+datasource+";initial catalog=restaurant;password="+password+";persist security info=True;user id="+account+";workstation id=ALUCARD;packet size=4096");
try
{
newconn.Open();
}
catch
{
return false;
}
finally
{
newconn.Close();
}
return true;
} public void ConnectionModify(string datasource,string account,string password)
{
//修改数据库连接字符串
sqlconn.ConnectionString="data source="+datasource+";initial catalog=restaurant;password="+password+";persist security info=True;user id="+account+";workstation id=ALUCARD;packet size=4096";
} public void GetCustomerLodgeLog(ref System.Windows.Forms.ListBox roomlog)
{
sqlcomm.CommandText="select sleepingroom.name,lodgelog.lodgedate,lodgelog.canceldate from sleepingroom,lodgelog where sleepingroom.id=lodgelog.sleepingroomid";
sqlconn.Open();
SqlDataReader reader=sqlcomm.ExecuteReader();
//MessageBox.Show(reader.FieldCount.ToString());
//string[] stringarray=new string[];
try
{
while(true)
{
string temp="";
reader.Read();
Hashtable table=new Hashtable();
temp+=TimeToDate(reader["lodgeDate"].ToString())+"\t\t";
temp+=TimeToDate(reader["cancelDate"].ToString())+"\t\t";
temp+=reader["name"].ToString();
//MessageBox.Show(temp+"\n"+temp.Length.ToString());
roomlog.Items.Add(temp);
}
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}
sqlconn.Close();
} private string TimeToDate(string time)
{
//把时间转换成等字长的日期字串
string result=time.Substring(0,time.IndexOf(" ",0,time.Length));
for (int i=0;i<10-result.Length;i++)
{
result+=" ";
}
//MessageBox.Show(result.Length.ToString());
return result;
}
}
}
using System;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace CustomerLodgingDemo
{
public class SqlLocator
{
[DllImport("odbc32.dll")]
private static extern short SQLAllocHandle(short hType, IntPtr inputHandle, out IntPtr outputHandle);
[DllImport("odbc32.dll")]
private static extern short SQLSetEnvAttr(IntPtr henv, int attribute, IntPtr valuePtr, int strLength);
[DllImport("odbc32.dll")]
private static extern short SQLFreeHandle(short hType, IntPtr handle);
[DllImport("odbc32.dll",CharSet=CharSet.Ansi)]
private static extern short SQLBrowseConnect(IntPtr hconn, StringBuilder inString,
short inStringLength, StringBuilder outString, short outStringLength,
out short outLengthNeeded); private const short SQL_HANDLE_ENV = 1;
private const short SQL_HANDLE_DBC = 2;
private const int SQL_ATTR_ODBC_VERSION = 200;
private const int SQL_OV_ODBC3 = 3;
private const short SQL_SUCCESS = 0;
private const short SQL_NEED_DATA = 99;
private const short DEFAULT_RESULT_SIZE = 1024;
private const string SQL_DRIVER_STR = "DRIVER=SQL SERVER";
private SqlLocator(){} public static string[] GetServers()
{
string[] retval = null;
string txt = string.Empty;
IntPtr henv = IntPtr.Zero;
IntPtr hconn = IntPtr.Zero;
StringBuilder inString = new StringBuilder(SQL_DRIVER_STR);
StringBuilder outString = new StringBuilder(DEFAULT_RESULT_SIZE);
short inStringLength = (short) inString.Length;
short lenNeeded = 0; try
{
if (SQL_SUCCESS == SQLAllocHandle(SQL_HANDLE_ENV, henv, out henv))
{
if (SQL_SUCCESS == SQLSetEnvAttr(henv,SQL_ATTR_ODBC_VERSION,(IntPtr)SQL_OV_ODBC3,0))
{
if (SQL_SUCCESS == SQLAllocHandle(SQL_HANDLE_DBC, henv, out hconn))
{
if (SQL_NEED_DATA == SQLBrowseConnect(hconn, inString, inStringLength, outString,
DEFAULT_RESULT_SIZE, out lenNeeded))
{
if (DEFAULT_RESULT_SIZE < lenNeeded)
{
outString.Capacity = lenNeeded;
if (SQL_NEED_DATA != SQLBrowseConnect(hconn, inString, inStringLength, outString,
lenNeeded,out lenNeeded))
{
throw new ApplicationException("Unabled to aquire SQL Servers from ODBC driver.");
}
}
txt = outString.ToString();
int start = txt.IndexOf("{") + 1;
int len = txt.IndexOf("}") - start;
if ((start > 0) && (len > 0))
{
txt = txt.Substring(start,len);
}
else
{
txt = string.Empty;
}
}
}
}
}
}
catch (Exception ex)
{
//Throw away any error if we are not in debug mode
#if (DEBUG)
MessageBox.Show(ex.Message,"Acquire SQL Servier List Error");
#endif
txt = string.Empty;
}
finally
{
if (hconn != IntPtr.Zero)
{
SQLFreeHandle(SQL_HANDLE_DBC,hconn);
}
if (henv != IntPtr.Zero)
{
SQLFreeHandle(SQL_HANDLE_ENV,hconn);
}
}
if (txt.Length > 0)
{
retval = txt.Split(",".ToCharArray());
} return retval;
}
}
}