您的位置:首页 > 数据库

对一段SQL进行语法检查

2010-03-16 22:26 323 查看
下面的C#代码用来检查一段SQL语句的语法是否正确:

public string CheckSQLCommand(string connectionString, string sql)
{
String message = null;

try
{
using (SqlConnection sqlConnection = new SqlConnection(connectionString))
{
sqlConnection.Open();
SqlCommand sqlCommand = sqlConnection.CreateCommand();
sqlCommand.CommandTimeout = 1;
sqlCommand.CommandText = "SET PARSEONLY ON;" + sql + ";SET PARSEONLY OFF;";
sqlCommand.ExecuteNonQuery();
}
}
catch (Exception ex)
{
if (ex.Message.IndexOf("Timeout") == -1)
{
SqlException sqlException = ex as SqlException;
if (sqlException != null)
{
if (sqlException.Number < 50000 && sqlException.Number != 2627)
{
message = ex.Message;
}
}
}
}

return message;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐