有关在WPF中连接到数据库的问题

时间:2012-11-17 00:02:16

标签: wpf database-connection

我有一个WPF应用程序,它将使用自定义类连接到数据库。

public class SQLRead
{
    public string sql;
    SqlConnection conn;
    SqlCommand cmd;

    public SQLRead()
    {
        sql = string.Empty; 
    }

    public void NewConnection()
    {
        conn = new SqlConnection(
            @"Data Source = Server\Instance;   
              Integrated Security = SSPI;");
        cmd = new SqlCommand(sql, conn);
        cmd.CommandTimeout = 120;
        cmd.CommandType = CommandType.Text;
    }

    public void Connect()
    {
        conn.Open();
    }

    public void Disconnect()
    {
        conn.Close();
    }

    //public void addParameterVarChar(string s, int size, object value)
    //{
    //    SqlParameter sp = new SqlParameter("@SubRegion", SqlDbType.Char, 1, "B");
    //    //sp.Value = value;
    //    cmd.Parameters.Add(sp);
    //}

    public void AddParameter(string s, object value)
    {
        SqlParameter sp = new SqlParameter(s, value);
        cmd.Parameters.Add(sp);
    }

    public List<string> ReadAlnName()
    {
        Debug.WriteLine(conn.ConnectionString);

        sql = @"Select AlignmentName + '::AlnID' + cast(AlnID as varchar(10))
                From [crwdb].[dbo].[Alignment]";
        cmd.CommandText = sql;

        var list = new List<string>();
        using (conn)
        {
            conn.Open();
            using (SqlDataReader reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    list.Add((string)reader.GetString(0));
                }
            }
        }
        conn.Close();
        //conn.Dispose();

        return list;
    }

    public static int ParseAlnEntry(string alnEntry)
    {
        Regex reg = new Regex(@"^.+?\:{2}AlnID(\d+)$");
        Match m;
        int alnID = -1;
        if ((m = reg.Match(alnEntry)).Success)
        {
            alnID = Convert.ToInt32(m.Groups[1].Value);
        }

        return alnID;
    }

    public List<PhylNode> ReadSeqTaxTree(int alnID, int seqTypeId, int locationID, int rootTaxID, int depth)
    {
        Debug.WriteLine(string.Format("\n{0}\n{1}",conn.ConnectionString, sql));

        // SQL query
        sql = @"Execute [crwdb].[dbo].[sp_SeqDistribution] 
                    @AlnID, @SeqTypeID, @LocationID, @RootTaxID, @Depth";
        cmd.CommandText = sql;

        // Add parameters
        AddParameter("@AlnID", alnID);
        AddParameter("@SeqTypeID", seqTypeId);
        AddParameter("@LocationID", locationID);
        AddParameter("RootTaxID", rootTaxID);
        AddParameter("Depth", depth);

        var tree = new List<PhylNode>();

        using (conn)
        {
            conn.Open();
            using (SqlDataReader reader = cmd.ExecuteReader())
            {
                int tmpTaxID, tmpParentID, tmpSeqNum;
                string tmpLineage;
                while (reader.Read())
                {
                    tmpTaxID = (int)reader.GetInt32(0);
                    tmpParentID = (int)reader.GetInt32(1);
                    tmpLineage = (string)reader.GetString(2);
                    tmpSeqNum = (int)reader.GetInt32(3);
                    if (tmpSeqNum > 0)
                    {
                        tree.Add(new PhylNode(tmpTaxID, tmpParentID, tmpLineage, tmpSeqNum));
                    }
                }
            }
        }
        conn.Close();
        //conn.Dispose();

        return tree;
    }
}

我创建一个SQLRead实例,将其传递给下一个窗口(通过构造函数),然后调用ReadAlnName方法,它可以工作。

public partial class DBModeMainWindow : Window
{
    // The database connection
    SQLRead Conn;

    // The phylogenetic tree
    List<PhylNode> tree;

    public DBModeMainWindow(SQLRead conn)
    {
        InitializeComponent();
        Conn = conn;

        List<string> alnStr = Conn.ReadAlnName();
        AlnViewModel alnVM = new AlnViewModel(alnStr);
        DataContext = alnVM;
    }

    private void buttonGetTree_Click(object sender, RoutedEventArgs e)
    {
        string selectedAlnEntry = comboBoxAln.SelectedValue.ToString();
        int alnID = SQLRead.ParseAlnEntry(selectedAlnEntry);
        MessageBox.Show(string.Format(
            "Selected item: {0}. \nAlnID:{1}", selectedAlnEntry, alnID));

        tree = Conn.ReadSeqTaxTree(1, 1, 1, 2, 3);
    }

比我添加一个按钮,当用户点击按钮时,后面的代码将调用Conn.ReadSeqTaxTree方法。但这一次,我收到一个错误“connectionString没有初始化。”

我错过了什么吗?任何建议将不胜感激。

0 个答案:

没有答案