Lm th? no ? s? d?ng t?p tin d? li?u lin k?t v?i cc ?i t?ng OleDbConnection trong Visual C#.NET

D?ch tiu ? D?ch tiu ?
ID c?a bi: 308426 - Xem s?n ph?m m bi ny p d?ng vo.
Bung t?t c? | Thu g?n t?t c?

? Trang ny

TM T?T

Bi vi?t ny ch?ng t? lm th? no ? s? d?ng t?p tin OLE DB Universal Data Link (.udl) ? ch? ?nh k?t n?i chu?i m m?t ADO.NET OleDbConnection ?i t?ng s? d?ng ? thi?t l?p k?t n?i c s? d? li?u.

T?p tin OLE DB Universal Data Link (.udl) cung c?p m?t cch thu?n ti?n ? t?o ra v xc minh m?t chu?i k?t n?i ActiveX Data Objects (ADO) ?c s? d?ng ? k?t n?i v?i m?t ngu?n d? li?u OLE DB. Thng tin chu?i k?t n?i ti?p t?c t?n t?i trong m?t ?nh d?ng c? th? vo m?t t?p tin v?i ph?n m? r?ng .udl. B?n c th? tham kh?o m?t t?p tin .udl trong cc ConnectionString ti s?n c?a m?t ADO.NET OleDbConnection ?i t?ng ? xc ?nh chu?i k?t n?i cc ?i t?ng nn s? d?ng ? k?t n?i v?i c s? d? li?u. S? d?ng cc Tn tp tham s? chu?i k?t n?i ? ch? tn c?a t?p tin .udl c ch?a thng tin chu?i k?t n?i.

N l quan tr?ng c?n lu ? cc thng tin sau y ?c l?y t? bi bo "OleDbConnection.ConnectionString Property" trong cc.NET Framework l?p th vi?n ti li?u (xem cc Tham khao ph?n):
? tham kh?o m?t lin k?t d? li?u Microsoft (UDL), thm m?t tham chi?u ?n UDL ? cc ConnectionString trong cc h?nh th?c "tn File = myfile.udl". N?u nhi?u hn m?t UDL ?c xc ?nh trong chu?i k?t n?i, ch? l s? xu?t hi?n cu?i cng ?c n?p. N?u b?n ch? ?nh m?t UDL, v sau g?i cho cc OleDbConnection ?i t?ng ? l?y cc ConnectionString, cc ConnectionString ch?a tham chi?u ?n cc t?p tin UDL, khng ph?i cc n?i dung c?a t?p tin. K?t n?i dy c ch?a UDLs ?c phn tch m?i l?n cc OleDbConnection ?c m? ra. B?i v? i?u ny c th? ?nh h?ng ?n hi?u su?t, m?t ConnectionString m hi?n khng tham kh?o m?t UDL ?c khuy?n khch.

Cc b?c ? xy d?ng cc m?u

Th?c hi?n theo cc b?c sau ? t?o ra m?t Visual C#.NET giao di?n i?u khi?n ?ng d?ng s? d?ng m?t t?p tin .udl ? xc ?nh thng tin chu?i k?t n?i cho m?t ADO.NET OleDbConnection ?i t?ng:
  1. S? d?ng cc SQL Server OLE DB Provider ? k?t n?i v?i m?t trong c s? d? li?u Microsoft SQL Server c?a b?n v t?o m?t t?p tin .udl ?c ?t tn Test.udl trong th m?c g?c c?a ? C.

    B?n c?ng c th? s? d?ng cc Microsoft OLE DB Provider cho my bay ph?n l?c 4,0 ? c?u h?nh t?p tin .udl ? k?t n?i v?i m?t Microsoft truy c?p 97 ho?c c s? d? li?u nm 2000 n?u b?n khng c quy?n truy c?p vo c s? d? li?u SQL Server.? bi?t thm chi ti?t v? lm th? no ? t?o m?t t?p tin .udl, b?m vo s? bi vi?t d?i y ? xem bi vi?t trong c s? ki?n th?c Microsoft:
    189680 Lm th? no ? s? d?ng lin k?t d? li?u t?p tin v?i ADO
  2. M? m?t m?i Visual C#.NET giao di?n i?u khi?n ?ng d?ng.
  3. Thay th? m? m?c ?nh trong cc m-un Class1 v?i o?n m? sau:
    using System;
    using System.Data;
    using System.Data.OleDb;
    
    namespace DataLink
    {
        class Class1
        {
            static void Main(string[] args)
            {
                OleDbConnection myConnection = new OleDbConnection("File Name = d:\\test.udl");
    
                try
                {
                    myConnection.Open();
                    if (myConnection.State == ConnectionState.Open)
                        Console.WriteLine("Connection opened successfully!");
                    else
                        Console.WriteLine("Connection could not be established");
                }
                catch(Exception ex)
                {
                    Console.WriteLine(ex.Message.ToString());
                }
                finally
                {
                    myConnection.Close();
                }
                Console.ReadLine();
     
                return;
            }
        }
    }
    					
  4. Trong tuyn b? r?ng b? cc ConnectionString ti s?n c?a cc OleDbConnection ?i t?ng, s?a ?i ?ng d?n ?n t?p tin .udl n?u b?n t?o ra n v?i m?t tn khc nhau ho?c ti?p t?c t?n t?i sang v? tr khc.
  5. Ch?y cc ?ng d?ng t? bn trong nh?ng tr?c quan Studio.NET tch h?p pht tri?n mi tr?ng (IDE). M?t c?a s? giao di?n i?u khi?n nn m? v hi?n th? vn b?n, "K?t n?i m? thnh cng!" ? ch? ra r?ng cc OleDbConnection ?i t?ng ? c th? s? d?ng thng tin chu?i k?t n?i trong t?p tin .udl ? thi?t l?p k?t n?i c s? d? li?u.
  6. H?y b?m ENTER ? b? qua c?a s? giao di?n i?u khi?n v ng?ng cc ?ng d?ng.

THAM KH?O

? bi?t thm thng tin v? cc OleDbConnection.ConnectionString b?t ?ng s?n, xem sau.NET Framework l?p th vi?n ti li?u:
http://MSDN.Microsoft.com/en-US/Library/System.Data.oledb.oledbconnection.connectionstring.aspx
? bi?t thm chi ti?t, b?m vo s? bi vi?t d?i y ? xem bi vi?t trong c s? ki?n th?c Microsoft:
189680 Lm th? no ? s? d?ng lin k?t d? li?u t?p tin v?i ADO

Thu?c tnh

ID c?a bi: 308426 - L?n xem xt sau cng: 27 Thang Tam 2011 - Xem xt l?i: 2.0
p d?ng
  • Microsoft ADO.NET 1.1
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft Visual C# .NET 2003 Standard Edition
T? kha:
kbhowtomaster kbsystemdata kbmt KB308426 KbMtvi
My d?ch
QUAN TRONG: Bi vi?t ny ?c d?ch b?ng ph?n m?m d?ch my c?a Microsoft ch? khng ph?i do con ng?i d?ch. Microsoft cung c?p cc bi vi?t do con ng?i d?ch v c? cc bi vi?t do my d?ch ? b?n c th? truy c?p vo t?t c? cc bi vi?t trong C s? Ki?n th?c c?a chng ti b?ng ngn ng? c?a b?n. Tuy nhin, bi vi?t do my d?ch khng ph?i lc no c?ng hon h?o. Lo?i bi vi?t ny c th? ch?a cc sai st v? t? v?ng, c php ho?c ng? php, gi?ng nh m?t ng?i n?c ngoi c th? m?c sai st khi ni ngn ng? c?a b?n. Microsoft khng ch?u trch nhi?m v? b?t k? s? thi?u chnh xc, sai st ho?c thi?t h?i no do vi?c d?ch sai n?i dung ho?c do ho?t ?ng s? d?ng c?a khch hng gy ra. Microsoft c?ng th?ng xuyn c?p nh?t ph?n m?m d?ch my ny.
Nh?p chu?t vo y ? xem b?n ti?ng Anh c?a bi vi?t ny:308426

Cung cp Phan hi

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com