Configuring Connection Strings

.NET application မှာ Connection String ကြေငြာတဲ့အခါမှာ အောက်ဖော်ပြပါ နည်းလမ်းနှစ်မျိုးကို အသုံးများကြပါတယ်။

1) Connection String from Project Settings

အောက်ပါ Steps အတိုင်းလိုက်လုပ်ပေးပါ။

  • ကိုယ့်ရဲ့ Project ကိုဖွင့်ပြီး Project Properties ကိုသွားပါ
  • Settings Tab ကိုဖွင့်ပါ
  • Type Column မှ dropdown တွင် (Connection String) ကိုရွေးပါ
  • Value Column မှ Button ကိုနှိပ််ပါ၊ Connection Properties Window ပွင့်လာပါလိမ့်မယ်။
  • သက်ဆိုင်ရာ Server Name ကိုရွေးပါ
  • Window Authentication ကိုသုံးမယ်ဆိုရင် ဘာမှ ထည့်ပေးစရာမလိုပါဘူး
  • SQL Server Authentication အတွက် တော့ User Name and Password ကိုထည့်ပါ
  • ပြီးတော့ ကိုယ်သုံးမယ် Database Name ကို Select or enter a database name drown down မှာရွေးပေးပါ
  • အားလုံးပြီးသွားရင် OK Button ကိုနှိပ်ပါ။ Web Application ဆိုရင် ConnectionStirng ကို Web.Config ထဲတွင် generate လုပ်ပေးပြီး Window Application ဆိုရင်တို့ App.Config file ထဲမှာ generate  လုပ်ပေးပါတယ်။

2) Connection String by manual declaration at Web.config

<connectionStrings>
<add name="myDBConn1" connectionString="Data Source=DBServer;Initial Catalog=DBName;Integrated Security=True" />
<add name="myDBConn2" connectionString=Data Source=DBServer;Initial Catalog=DBName;User;Password=Password" />
</connectionStrings>

myDBConn1 က Window Authentication ဖြစ်ပြီးတော့ myDBConn2 ကတော့ UserId နဲ့ Passowrd သုံးထားတဲ့ SQL Authentication ပါ။ အခုကြေငြာခဲ့တဲ့ Connection တွေကို Project မှာ ခေါ်သုံးမယ်ဆိုရင်တော့အောက်ပါ namespace နှစ်ခုကို အရင် import လုပ်ပေးပါ။

using System.Configuration;
using System.Data.SqlClient;

ဒါဆိုရင် သူငယ်ချင်းရဲ့ Program မှာဒီလိုခေါ်သုံးပြီး Connection ကို establish လုပ်နိုင်ပါပြီ

SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["myDb"].ConnectionString);
myConnection.Open();

Facebook comments:

8 Responses

  1. ProNek says:

    ဘယ်လိုပဲဖြစ်ဖြစ် တန်ဘိုးရှိပါတယ်ဗျာ။ စေတနာ – ကံ မည်၏။

  2. Shin Thant says:

    အခုလို ဓမ္မသဘောနဲ့ ဖြည့်စွက်ပေးလို့ အရမ်း ကျေးဇူးတင်ပါတယ်။ စာဖတ်သူအတွက် တခုခုအကျိုးပြုသွားရင် ရေးရကျိုးနပ်ပါပြီ။

  3. Devtech says:

    As for the beginner , like that article is very effective tool to be broaden their horizons. I am very keen to read your next post. Thanks for all.

    • Shin Thant says:

      Those are not only for beginners but also for the experience .NET developers which need to memorize at all times. Thanks you yr comments. Sure, I have been writing many articles covering all the fundamental stuffs about ASP.NET. I’ll be posting them regularly.

  4. dev2709 says:

    Saving connection string as plain text in web config is not a good idea. connection string should be encrypted.

    • Shin Thant says:

      I like this kind of idea. Thanks! What I think is that either the developers or the project related members could access to the web server. For that, I make the Connection variables accessible to read.

      Being supported to your idea, I’ll continue adding about the encrypted connection string at this post since this is still a short one. Looking forward your further suggestions at my future posts.

  5. chanmyae says:

    တော်တော်လေးပြည့်စုံသွားအောင် အခုလို settings ကနေ ပြန်ယူသုံးမယ့်ပုံစံကလေးထည့်ရင်ကောင်းမလားလို့ပါ
    string connectionString = Properties.Settings.Default.CurrentConnection.Name;

    • Shin Thant says:

      အခုလို အကြံပြုတာ ကျေးဇူးတင်ပါတယ်။ ဒီ Post မှာ Conenction String ကို Encrypt လုပ်တဲ့ပုံစံရယ် ပြီးတော့ ချမ်းမြေ့ပြောတဲ့ ပုံစံကိုပေါင်းထည့်ပါဦးမယ်။ လောလောဆယ် SharePoint article တွေနဲ့ အလုပ်များနေလို့ပါ။

Leave a comment


*