C#Configuration Manager连接字符串

时间:2015-12-10 18:47:21

标签: c# connection-string configurationmanager

所以我正在尝试做一个从帐户1到帐户2进行简单交易的应用程序

这是代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Transactions;

namespace BankTransactions
{
    public class Transactions
    {
        public bool Transfer(int fromAcno, int toAcno, decimal amt)
        {
            try
            {
                string cs = ConfigurationManager.ConnectionStrings["Integrated Security = SSPI; Persist Security Info = False; Initial Catalog = NORTHWND; Data Source = CRISTI"].ConnectionString;
                SqlConnection cn = new SqlConnection(cs);
                cn.Open();
                SqlTransaction trans;
                trans = cn.BeginTransaction();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = cn;
                cmd.Transaction = trans;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = String.Format("update PSBank set amount=amount+{0} where accountno={1}", amt, toAcno);
                cmd.ExecuteNonQuery();
                cmd.CommandText = String.Format("update PSBank set amount=amount-{0} where accountno={1}", amt, fromAcno);
                cmd.ExecuteNonQuery();
                decimal balance;
                balance = (decimal)cmd.ExecuteScalar();
                decimal originalBalance;
                originalBalance = balance - amt;
                bool flag = true;
                if (originalBalance >= 5000)
                {
                    trans.Commit();
                    flag = true;
                }

                else
                {
                    trans.Rollback();
                    flag = false;
                }

                cn.Close();
                return flag;
            }

            catch (Exception ex)
            {
                return false;
            }
        }


    }
}

这是winform代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BankTransactions;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            int fromAcno = int.Parse(txtFromAcno.Text);
            int toAcno = int.Parse(txtToAcno.Text);
            decimal amount = decimal.Parse(txtAmount.Text);

            Transactions obj = new Transactions();
            if (obj.Transfer(fromAcno, toAcno, amount) ==true)
            {
                MessageBox.Show("Amount transferred succesfully!", "Confirm");
            }
            else
            {
                MessageBox.Show("Error!", "Error");
            }
        }
    }
}

表单有3个文本框:txtFromAcno,txtToAcno和txtAmount。 我有一个名为NORTHWND的数据库和表PSBank,我有这两个帐户。 我收到错误消息,而不是“转移成功”。我不知道如何在连接字符串中建立与DB的连接。

  string cs = ConfigurationManager.ConnectionStrings["Integrated Security = SSPI; Persist Security Info = False; Initial Catalog = NORTHWND; Data Source = CRISTI"].ConnectionString;

如何正确格式化?

1 个答案:

答案 0 :(得分:1)

将连接字符串放在app.config中,如下所示:

<connectionStrings>
    <add name="MyConnection" 
        connectionString="Integrated Security = SSPI; Persist Security Info = False; Initial Catalog = NORTHWND; Data Source = CRISTI"
        providerName="System.Data.SqlClient" >
</connectionStrings>

然后在代码中按名称引用它:

string cs = ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString;