C # program to connect the database and read the fields in the database simple method Summary _c# tutorial

Source: Internet
Author: User
Tags oracleconnection string format create database oracle database

Connect to an Access database

string connstr = @ "Data Source=d:\database.mdb; provider=microsoft.jet.oledb.4.0; "; Database connection string 
OleDbConnection conn = new OleDbConnection (CONNSTR); 

Connecting Oracle Databases

The database and the client are using System.Data.OracleClient on the same machine 
; 
String connstr = "Data source=orcl;user=user;password=pwd;"; 
OracleConnection conn = new OracleConnection (CONNSTR); 
The client and database are not on the same machine, you can use System.Data.OleDb using the following connection method 
; 
Host is the Oracle database server address and port is the Oracle database port, service_name is the database name 
string connstr = "Provider=oraoledb.oracle.1;data Source= (DESCRIPTION = (Address_list = (address = (PROTOCOL = TCP) (HOST = 127.0.0.1) (PORT = 1521)) (Connect_data = (service_n AME = ORCL))); User Id=message; Password=message; "; 
OleDbConnection conn = new OleDbConnection (CONNSTR); 

Connecting to the SQL Server database

Using System.Data.SqlClient; 
String connstr = "Data source=127.0.0.1;initial catalog=database;user Id=sa;pwd=sa"; 
SqlConnection conn = new SqlConnection (CONNSTR); 

Connect the database and read the data from the database and output it!

Using System; 
Using System.Collections.Generic; 
Using System.Linq; 
Using System.Text; 
 
Using System.Data.SqlClient; Namespace Login {class Program {static void Main (string[] args) {//Create a new database connection using (SqlConnection con n = new SqlConnection (Getconnectstring ())) {Conn. 
    Open ()//Opens database//console.writeline ("Database open successfully!"); Create DATABASE command SqlCommand CMD = conn. 
    CreateCommand (); 
    Create a query statement cmd.commandtext = "SELECT * from UserInfo"; Read the data stream from the database into reader sqldatareader reader = cmd.     
     
    ExecuteReader (); Read the next line of data from reader, if there is no data, reader. Read () returns flase while reader. Read ()) {//reader. GetOrdinal ("id") is the index,//reader, of the column that gets the ID. GetInt32 (int n) This is to return the nth column of data in INT32 format to//reader. GetString (int n) This is to return the nth column of data to the int id = Reader in string format. GetInt32 (reader. 
     GetOrdinal ("id")); String name = Reader. GetString (reader. 
     GetOrdinal ("name")); string pwd = reader. GetString (reader. 
     GetOrdinal ("password")); Int Age = Reader. GetInt32 (reader. 
     GetOrdinal ("Age")); string sex = reader. GetString (reader. 
     GetOrdinal ("Sex")); string phone = reader. GetString (reader. 
     GetOrdinal ("Phone")); string address = reader. GetString (reader. 
 
     GetOrdinal ("Address")); Format output Data Console.Write ("id:{0},name:{1},pwd:{2},age:{3},sex:{4},phone{5},address:{6}\n", ID, Name, PWD, age, Sex, ph 
    one, address); 
  } console.readkey (); ///Get a database connection string, static string Getconnectstring () {return ' Data source= (local); Initial catalog=db1;integrate 

 D Security=sspi; ";}}}


determine if the value of the field fetched in the database is null (NULL)
the last time you manipulate a database, you need to determine whether the returned field value is empty and search for three methods on Google.

1 through System.DBNull judgment, most of the online use this method.

DataTable DT;        Assuming the field name, DT has saved the data 
dt.rows[0]["name" = = System.DBNull.Value;//To determine whether the first row of data is empty 

2 Judging by IsNull

DataTable DT;    Assuming the field name, DT has saved the data 
dt.rows[0]. IsNull ("name"); Determine if the name field of the first row of data is empty 

3 Judging by ToString (), I have not tried this method.

DataTable DT;       Assuming the field name, DT has saved the data 
dt.rows[0]["name". ToString () = = ""; Determine if the name field of the first row of data is empty 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.