Summary of Simple Methods of Connecting Database and Reading Fields in Database by C Program
- 2021-10-11 19:22:08
- OfStack
Connecting 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 to an Oracle database
// Database and client are in the same 1 On a machine
using System.Data.OracleClient;
string connStr = "data source=orcl;user=user;password=pwd;";
OracleConnection conn = new OracleConnection(connStr);
// Client and database are not the same 1 Machine, you can use the following connection methods
using System.Data.OleDb;
//HOST Yes Oracle Database server address, PORT Yes 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_NAME = orcl)));User ID=message;Password=message;";
OleDbConnection conn = new OleDbConnection(connStr);
Connecting to an SqlServer 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 to 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)
{
// New 1 Database connections
using(SqlConnection conn = new SqlConnection(GetConnectString()))
{
conn.Open();// Open the database
//Console.WriteLine(" Database opened successfully !");
// Create database command
SqlCommand cmd = conn.CreateCommand();
// Create a query statement
cmd.CommandText = "SELECT * FROM userinfo";
// Read the data stream from the database and store it in reader Medium
SqlDataReader reader = cmd.ExecuteReader();
// From reader Read the following in 1 Row data , If there is no data ,reader.Read() Return flase
while (reader.Read())
{
//reader.GetOrdinal("id") Is to get ID Of the column in which the index,
//reader.GetInt32(int n) This is the first n The data of the column is used in the Int32 Returns the format of
//reader.GetString(int n) This is the first n The data of the column is used in the string Format return
int id = reader.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, phone, address);
}
}
Console.ReadKey();
}
// Get 1 Database connection strings
static string GetConnectString()
{
return "Data Source=(local);Initial Catalog=db1;Integrated Security=SSPI;";
}
}
}
Determine whether the field value taken out in the database is empty (NULL)
Recently, when operating the database, it is necessary to judge whether the returned field value is empty. There are three methods searched on Google.
1 Judging by System. DBNull, this method is mostly used on the Internet.
DataTable dt; // Assume that the field is name, dt The data has been saved
dt.rows[0]["name"] == System.DBNull.Value; // Judgement number 1 Of row data name Whether the field is empty
2 Judged by IsNull
DataTable dt; // Assume that the field is name, dt The data has been saved
dt.rows[0].IsNull("name"); // Judgement number 1 Of row data name Whether the field is empty
3 Judging by ToString (), I have not tried this method.
DataTable dt; // Assume that the field is name, dt The data has been saved
dt.rows[0]["name"].ToString() == ""; // Judgement number 1 Of row data name Whether the field is empty