標籤:os strong 資料 io art for
建立串連分為六步:
1.定義連接字串,oracle 的連接字串為:
private static string connString = "Data Source=192.168.1.130:1521/mydata;Persist Security Info=True;User ID=em_test;Password=test123;Unicode=True";
2.根據連接字串成串連
3.開啟串連
4.執行個體命令對象
5.操作
6.關閉串連
看一個例子:
using System; using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OracleClient;
namespace OpenCloseDB
{
public partial class Form1 : Form
{
public Form1()
{ InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OracleConnection connection = null;
int num = 0;
string message = "";
String sql = "select count(*) from ex_user";
try
{
string connString = "Data Source=mydata;Persist Security Info=True;User ID=test;Password=huikk123;Unicode=True";
// 建立connection對象
connection = new OracleConnection(connString);
// 開啟串連
connection.Open();
OracleCommand command = new OracleCommand(sql, connection);
num = int.Parse(command.ExecuteScalar().ToString());
message = string.Format("共有{0}條使用者資訊!", num);
MessageBox.Show(message);
}
catch (Exception ex)
{
MessageBox.Show("出現異常"+ex.Message);
}
finally
{
if (connection != null)
{
connection.Close();
}
}
}
}
}