OLEDB连接无法打开 [英] OLEDB connection won't open

查看:160
本文介绍了OLEDB连接无法打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在使用VS2010连接到远程计算机上的SQLServer 2000实例.
我正在使用无法打开的OLEDB连接字符串.我想知道是否有人可以阐明为什么这种连接无法打开.任何帮助是极大的赞赏.谢谢!
下面的代码.

Hi All,

I am using VS2010 to connect to a SQLServer 2000 instance on a remote machine.
I am using a OLEDB connection string that just will not open. I''m wondering if anyone can shed some light on why this connection just won''t open. Any help is greatly appreciated. Thank you!
Code below.

public string RDDTConnectionString = @"Provider=sqloledb;Data Source=MyServer;Initial Catalog=MyDataBase;Integrated Security=SSPI;";

public OleDbConnection ConnString() 
{
    OleDbConnection connString = new OleDbConnection();
    connString.ConnectionString = RDDTConnectionString;
    return connString;
}

private void btnSaveDealSheet_Click(object sender, EventArgs e)
{
    try
    {
        if (ConnString().State == ConnectionState.Closed)
        {
            ConnString().Open();                   
            MessageBox.Show("ConnString.State:" + ConnString().State);
        }
        //Execute SQL
        OleDbCommand InsertCommand = GetInsertDealSheetCommand();
        InsertedID = Convert.ToInt32(InsertCommand.ExecuteScalar());
        //Return inserted Identity
        MessageBox.Show("DealSheet Successfully Added");
    }
    catch (Exception ex) 
    {
        MessageBox.Show("error: " + ex.Message); ;
    }
}

public void OpenConnectionString()
{           
    try
    {
        ConnString().Open();

        if (ConnString().State == ConnectionState.Open)
        {
            MessageBox.Show("opened");
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error: " + ex.Message); 
    }
}

推荐答案

是否可以将其切换为使用 ^ ]-这种方法效率更高

您的连接字符串将为Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI; 1

1 来自 www.connectionstrings.com [
Can you switch it to use a SqlConnection[^] - This sould be more efficient

Your connection string would then be Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI;1

1Taken from www.connectionstrings.com[^]


我认为我发现了您的错误-您将ConnString()方法多次创建一个新的连接实例.

您的代码应为

I think I have spotted your error - you call the ConnString() method multiple times creating a new connection instance each time.

Your code should be

public string RDDTConnectionString = @"Provider=sqloledb;Data Source=MyServer;Initial Catalog=MyDataBase;Integrated Security=SSPI;";


public OleDbConnection ConnString() 
{
    OleDbConnection connString = new OleDbConnection();
    connString.ConnectionString = RDDTConnectionString;
    return connString;
}
 
private void btnSaveDealSheet_Click(object sender, EventArgs e)
{
    try
    {
        OleDbConnection connString = ConnString() ; // added
        if (connString.State == ConnectionState.Closed) //changed
        {
            connString.Open();// changed                   
            MessageBox.Show("ConnString.State:" + connString.State);//changed
        }
        //Execute SQL
        OleDbCommand InsertCommand = GetInsertDealSheetCommand(); // check this method to be sure you don't create a new connection instance
        InsertedID = Convert.ToInt32(InsertCommand.ExecuteScalar());
        //Return inserted Identity
        MessageBox.Show("DealSheet Successfully Added");
    }
    catch (Exception ex) 
    {
        MessageBox.Show("error: " + ex.Message); ;
    }
}
 
public void OpenConnectionString()
{           
    try
    {
        OleDbConnection connString = ConnString() ; // added
        connString.Open(); //changed
 
        if (connString.State == ConnectionState.Open)
        {
            MessageBox.Show("opened");
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error: " + ex.Message); 
    }
}


这篇关于OLEDB连接无法打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆