首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >捕获到Sql异常

捕获到Sql异常
EN

Stack Overflow用户
提问于 2010-12-01 13:50:37
回答 1查看 139关注 0票数 1

我有一个这样的方法

代码语言:javascript
复制
        public static DataSet GetAllDataBaseNames()
        {

        //Instance of connection is created
        SqlConnection sConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);

        //To Open the connection.
        sConnection.Open();
        string selectDatabase = @"SELECT   
                                        [NAME]
                                    FROM     
                                        [master..sysdatabases]";

        //Instance of command is created.
        SqlCommand sCommand = new SqlCommand(selectDatabase, sConnection);

        try
            {
            //Create the dataset.
            DataSet dsListOfDatabases = new DataSet("master..sysdatabases");

            //Create the dataadapter object.
            SqlDataAdapter da = new SqlDataAdapter(selectDatabase, sConnection);

            //Provides the master mapping between the sourcr table and system.data.datatable
            da.TableMappings.Add("Table", "master..sysdatabases");

            //Fill the dataadapter.
            da.Fill(dsListOfDatabases);

            //Bind the result combobox with non primary key table names
            DataViewManager dsv = dsListOfDatabases.DefaultViewManager;
            return dsListOfDatabases;
            }
        catch(Exception ex)
            {
            //Handles the exception and log that to the EventLog with the original message.
            EventLog log = new EventLog("Application");
            log.Source = "MFDBAnalyser";
            log.WriteEntry(ex.Message);
            return null;
            }

        finally
            {
            //checks whether the connection is still open.
            if(sConnection.State != ConnectionState.Closed)
                {
                sConnection.Close();
                }
            }

        }

但是当我这样叫它的时候

代码语言:javascript
复制
        public void BindDBDropDown()
        {

          DataSet dsTablesWithoutForeignKeys = default(DataSet);

        try
            {
            //The function GetAllForeignKeyTables() is called from the class PluginManager.
            dsTablesWithoutForeignKeys = DataAccessMaster.GetAllDataBaseNames();

            dgResultView.DataSource = dsTablesWithoutForeignKeys.Tables["master..sysdatabases"];
            }
        catch(Exception ex)
            {
            //All the exceptions are handled and written in the EventLog.
            EventLog logException = new EventLog("Application");
            logException.Source = "MFDBAnalyser";
            logException.WriteEntry(ex.Message);
            }
        }

它正在获取在catch块中捕获的sql异常

你们能看看这个吗。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-12-01 14:02:13

尝试:

代码语言:javascript
复制
SELECT   [name]
FROM     [master]..[sysdatabases] // **not** [master..sysdatabases]

(请注意方括号)

此外,最好将using用于SqlConnectionSqlCommand等实现IDisposable的所有东西,这将确保您不会破坏连接。

代码语言:javascript
复制
using(var sConnection = new
    SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]))
{

    //To Open the connection.
    sConnection.Open();
    string selectDatabase = @"
        SELECT    [NAME]
        FROM    [master]..[sysdatabases]";

    //Instance of command is created.
    using(var sCommand = new SqlCommand(selectDatabase, sConnection)) {
         // etc
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4321592

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档