一,调用 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
);
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.
其次调用此API的C#形式:
StringBuilder shortTargetFile = new StringBuilder(80);
int result = GetShortPathName(targetFile, shortTargetFile, shortTargetFile.Capacity);
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.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();
把红色部分替换成你需要的存储过程名字即可
转载于:https://blog.51cto.com/chris/29179