您的位置:首页 > 职场人生

DBController开发后记--一些小的tips 推荐

2007-06-06 21:47 330 查看
一,调用 SQLDMO进行数据库备份的时候,容易如果备份的路径含有空格,就会失败,处理方式如下,这个是由于 SQLDMO本身不完备造成的
思想:调用API函数,把路径转为Dos模式
具体代码:
首先在类中声明此API
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern int GetShortPathName
(
[MarshalAs(UnmanagedType.LPTStr)]
  string path,
[MarshalAs(UnmanagedType.LPTStr)]
  StringBuilder shortPath,
int shortPathLength
);
关于GetShortPathName函数的信息如下:

GetShortPathName

The GetShortPathName function retrieves the short path form of the specified path.
DWORD GetShortPathName(
LPCTSTR lpszLongPath,
  LPTSTR lpszShortPath,
  DWORD cchBuffer);


Parameters

lpszLongPath 源数据字串
[in] Pointer to a null-terminated path string. The function retrieves the short form of this path.
lpszShortPath 目标数据字串,变短了
[out] Pointer to a buffer to receive the null-terminated short form of the path specified by lpszLongPath.
cchBuffer 目标数据字串的长度

[in] Size of the buffer pointed to by lpszShortPath, in TCHARs

Return Values

如果成功返回,返回值为目标字串的长度
If the function succeeds, the return value is the length, in TCHARs, of the string copied to lpszShortPath, not including the terminating null character.

ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.WIN32COM.v10.en/fileio/fs/getshortpathname.htm

其次调用此API的C#形式:
StringBuilder shortTargetFile = new StringBuilder(80);
int result = GetShortPathName(targetFile, shortTargetFile, shortTargetFile.Capacity);

objBackup.Files = shortTargetFile.ToString();
二:查找在Sql Server数据库中是否存在存储过程
代码如下:
SqlConnection sn = new SqlConnection("server=" + myServer + ";uid=" + myUser + ";pwd=" + myPassword + ";database=" + "");
sn.Open();
SqlCommand cmd = new SqlCommand(" SELECT 1 FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[p_killspid]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1",sn);

SqlDataReader mySqlRdr = cmd.ExecuteReader();
while (mySqlRdr.Read())
{
//exist
mySqlRdr.Close();
sn.Dispose();
return true;
}
sn.Close();
把红色部分替换成你需要的存储过程名字即可
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息