c#检查SQL语法是否正确,不执行SQL语句

public static bool ValidateSQL(string sql)

        {

            bool bResult;

 

            using (SqlConnection conn = new SqlConnection(DBHelper.ConnectionString))

            {

                using (SqlCommand cmd = new SqlCommand())

                {

                    if (conn.State != ConnectionState.Open)

                        conn.Open();

                    cmd.Connection = conn;

                    cmd.CommandText = "SET PARSEONLY ON";

                    // cmd.ExecuteNonQuery();  

                    try

                    {

                        cmd.CommandText = sql;

                        cmd.ExecuteNonQuery();

                        bResult = true;

                    }

                    catch (Exception ex)

                    {

                        bResult = false;

                    }

                    finally

                    {

                        cmd.CommandText = "SET PARSEONLY OFF";

                        cmd.ExecuteNonQuery();

                    }

                }

            }

            return bResult;

        }

public static bool ValidateSQL(string sql)

        {

            bool bResult;

 

            using (SqlConnection conn = new SqlConnection(DBHelper.ConnectionString))

            {

                using (SqlCommand cmd = new SqlCommand())

                {

                    if (conn.State != ConnectionState.Open)

                        conn.Open();

                    cmd.Connection = conn;

                    cmd.CommandText = "SET PARSEONLY ON";

                    // cmd.ExecuteNonQuery();  

                    try

                    {

                        cmd.CommandText = sql;

                        cmd.ExecuteNonQuery();

                        bResult = true;

                    }

                    catch (Exception ex)

                    {

                        bResult = false;

                    }

                    finally

                    {

                        cmd.CommandText = "SET PARSEONLY OFF";

                        cmd.ExecuteNonQuery();

                    }

                }

            }

            return bResult;

        }

相关文章:

  • 2021-06-27
  • 2022-12-23
  • 2021-07-25
  • 2021-12-05
  • 2021-09-22
  • 2021-05-28
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-09-20
  • 2022-12-23
  • 2022-12-23
  • 2021-06-13
  • 2021-10-08
  • 2022-02-12
相关资源
相似解决方案