Connect to Access Database
string connStr = @"Data Source=D:\; Provider=.4.0;"; //Database connection stringOleDbConnection conn = new OleDbConnection(connStr);
Connect to Oracle Database
//The database and client are on the same machineusing ; string connStr = "data source=orcl;user=user;password=pwd;"; OracleConnection conn = new OracleConnection(connStr); //The client and the database are not on the same machine, you can use the following connection methodusing ; //HOST is the Oracle database server address, PORT is the Oracle database port, SERVICE_NAME is the database namestring connStr = "Provider=.1;Data Source=(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521)))(CONNECT_DATA =(SERVICE_NAME = orcl)));User ID=message;Password=message;"; OleDbConnection conn = new OleDbConnection(connStr);
Connect to the SqlServer database
using ; string connStr = "data source=127.0.0.1;initial catalog=database;user id=sa;pwd=sa"; SqlConnection conn = new SqlConnection(connStr);
Connect to the database, read the data from the database and output it!
using System; using ; using ; using ; using ; namespace Login { class Program { static void Main(string[] args) { // Create a new database connection using(SqlConnection conn = new SqlConnection(GetConnectString())) { ();//Open the database //("The database is opened successfully!"); //Create a database command SqlCommand cmd = (); //Create a query statement = "SELECT * FROM userinfo"; //Read the data stream from the database and store it into the reader SqlDataReader reader = (); //Read the next row of data from the reader. If there is no data, () returns flase while (()) { //("id") is the index of the column where the ID is located, //reader.GetInt32(int n) This returns the data in column n in the format of Int32 //(int n) This is to return the data of column n in string format int id = reader.GetInt32(("id")); string name = (("name")); string pwd = (("password")); int age = reader.GetInt32(("age")); string sex = (("sex")); string phone = (("phone")); string address = (("Address")); //Format output data ("ID:{0},Name:{1},PWD:{2},Age:{3},Sex:{4},Phone{5},Address:{6}\n", id, name, pwd, age, sex, phone, address); } } (); } //Get a database connection string static string GetConnectString() { return "Data Source=(local);Initial Catalog=db1;Integrated Security=SSPI;"; } } }
Determine whether the field value retrieved in the database is empty (NULL)
Recently, when operating the database, you need to determine whether the returned field value is empty. Three methods have been searched on Google.
1 Through judgment, most of the online use this method.
DataTable dt; //Suppose the field is name, dt has saved the data[0]["name"] == ; //Judge whether the name field of the first line of data is empty
2 judged by IsNull
DataTable dt; //Suppose the field is name, dt has saved the data[0].IsNull("name"); //Judge whether the name field of the first line of data is empty
3 Judging by ToString(), I have not tried this method.
DataTable dt; //Suppose the field is name, dt has saved the data[0]["name"].ToString() == ""; //Judge whether the name field of the first line of data is empty