C# Oracle、Sql Server连接(增、删、改、查)
2014-01-08 09:36
148 查看
1、连接oracle,并可以将数据库的数据显示在 gridControl上
private void Btn_XS_Click(object sender, EventArgs e)
{
//写连接串
//ntegrated Security 身份验证方式
//当为false时,将在连接中指定用户ID和密码。
//当为true时,将使用当前的Windows帐户凭据进行身份验证。
//可识别的值为true、false、yes、no以及与true等效的sspi。
string ConnectionString = "Data Source=数据库名;User Id=用户名;Password=密码;Integrated Security=no;";
//创建一个新连接
OracleConnection conn=new OracleConnection(ConnectionString);
//以上两句也可以写成 OracleConnection conn=new OracleConnection "Data Source=数据库名;User Id=用户名;Password=密码;Integrated Security=no;");
try
{
conn.Open();
//下面这句话,即使是select....返回一个int类型的数,也要按下面这么利用数据集去做
//不可能用一句话来实现
//注意从函数外面传递参数到sql语句中的写法
//比如传递AdNumber
//"selectyhbh from gspuser where yhbh='" + AdNumber + "'"
OracleCommand cmd = new OracleCommand("select * from FY", conn);
OracleDataAdapter oda = new OracleDataAdapter();
oda.SelectCommand = cmd;
DataSet ds = new DataSet();
oda.Fill(ds);
//如果这想要第一行第一列可以这么写ds.Tables[0].Rows[0][0]
gridControl1.DataSource = ds.Tables[0].DefaultView;
conn.Close();
}
catch (Exception ee)
{
//如果有错误,输出错误信息
MessageBox.Show(ee.Message);
}
finally
{
//关闭连接
conn.Close();
}
}
static string MyConn = "server=127.0.0.1;uid=sa;pwd=密码;database=Text1;Trusted_Connection=no";
SqlConnection MyConnection = new SqlConnection(MyConn);
//此处的表名Table_1,数据库名Text1,表中一共有3列Name, Salary,id
//增加
private void button1_Click(object sender, EventArgs e)
{ //输入框
string MyInsert = "insert into Table_1(Name, Salary,id)values('" + Convert.ToString(textBox1.Text) + "','" + Convert.ToString(textBox2.Text) + "','" + Convert.ToString(textBox3.Text)+ "')";
SqlCommand MyCommand = new SqlCommand(MyInsert, MyConnection);
try
{
MyConnection.Open();
MyCommand.ExecuteNonQuery();
MyConnection.Close();
}
catch (Exception ex)
{
Console.WriteLine("{0} Exception caught.", ex);
}
}
//删除
private void button2_Click(object sender, EventArgs e)
{
string MyDelete = "Delete from Table_1 where id='" + textBox3.Text + "'";
//string MyDelete = "Delete from Table_1 where id='" + textBox1.Text + "'and Name='" + textBox1.Text + "'and Salary='+textBox3.Text+' ";
SqlCommand MyCommand = new SqlCommand(MyDelete, MyConnection);
try
{
MyConnection.Open();
MyCommand.ExecuteNonQuery();
MyConnection.Close();
}
catch (Exception ex)
{
Console.WriteLine("{0} Exception caught.", ex);
}
}
//更新
private void button3_Click(object sender, EventArgs e)
{
string Name = textBox1.Text;
string Salary = textBox2.Text;
string id = textBox3.Text;
string MyUpdate = "Update Table_1 set Name='" + Name + "',Salary='" + Salary + "' where id='" + textBox3.Text+"'";
SqlCommand MyCommand = new SqlCommand(MyUpdate, MyConnection);
try
{
MyConnection.Open();
MyCommand.ExecuteNonQuery();
MyConnection.Close();
}
catch (Exception ex)
{
Console.WriteLine("{0} Exception caught.", ex);
}
}
//查询数据
private void button4_Click(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection("server=(local);database=Text1;Uid=sa;Pwd=aaaaaa");
cn.Open();
SqlDataAdapter dap = new SqlDataAdapter("SELECT Name,Salary,id FROM Table_1", cn);
DataSet ds = new DataSet();//实例化DataSet类
dap.Fill(ds, "Table");//添加SQL语句并执行
dataGridView1.DataSource = ds.Tables[0].DefaultView;//显示数据
}
private void Btn_XS_Click(object sender, EventArgs e)
{
//写连接串
//ntegrated Security 身份验证方式
//当为false时,将在连接中指定用户ID和密码。
//当为true时,将使用当前的Windows帐户凭据进行身份验证。
//可识别的值为true、false、yes、no以及与true等效的sspi。
string ConnectionString = "Data Source=数据库名;User Id=用户名;Password=密码;Integrated Security=no;";
//创建一个新连接
OracleConnection conn=new OracleConnection(ConnectionString);
//以上两句也可以写成 OracleConnection conn=new OracleConnection "Data Source=数据库名;User Id=用户名;Password=密码;Integrated Security=no;");
try
{
conn.Open();
//下面这句话,即使是select....返回一个int类型的数,也要按下面这么利用数据集去做
//不可能用一句话来实现
//注意从函数外面传递参数到sql语句中的写法
//比如传递AdNumber
//"selectyhbh from gspuser where yhbh='" + AdNumber + "'"
OracleCommand cmd = new OracleCommand("select * from FY", conn);
OracleDataAdapter oda = new OracleDataAdapter();
oda.SelectCommand = cmd;
DataSet ds = new DataSet();
oda.Fill(ds);
//如果这想要第一行第一列可以这么写ds.Tables[0].Rows[0][0]
gridControl1.DataSource = ds.Tables[0].DefaultView;
conn.Close();
}
catch (Exception ee)
{
//如果有错误,输出错误信息
MessageBox.Show(ee.Message);
}
finally
{
//关闭连接
conn.Close();
}
}
//修改 string ConnectionString = "DataSource=ORCL;User Id=system;Password=aaaaaa;Integrated Security=no;"; OracleConnection conn=new OracleConnection(ConnectionString); conn.Open(); string str1 = "SQL修改语句'"; //当不传递conn时,会提示连接没有打开 OracleCommand cmd1 = new OracleCommand(str1,conn); //ExecuteNonQuery()对于Update,Insert,Delete 语句执行成功是返回值为该命令所影响的行数 int result=cmd1.ExecuteNonQuery();2、sql server连接,并实现增、删、改、查
static string MyConn = "server=127.0.0.1;uid=sa;pwd=密码;database=Text1;Trusted_Connection=no";
SqlConnection MyConnection = new SqlConnection(MyConn);
//此处的表名Table_1,数据库名Text1,表中一共有3列Name, Salary,id
//增加
private void button1_Click(object sender, EventArgs e)
{ //输入框
string MyInsert = "insert into Table_1(Name, Salary,id)values('" + Convert.ToString(textBox1.Text) + "','" + Convert.ToString(textBox2.Text) + "','" + Convert.ToString(textBox3.Text)+ "')";
SqlCommand MyCommand = new SqlCommand(MyInsert, MyConnection);
try
{
MyConnection.Open();
MyCommand.ExecuteNonQuery();
MyConnection.Close();
}
catch (Exception ex)
{
Console.WriteLine("{0} Exception caught.", ex);
}
}
//删除
private void button2_Click(object sender, EventArgs e)
{
string MyDelete = "Delete from Table_1 where id='" + textBox3.Text + "'";
//string MyDelete = "Delete from Table_1 where id='" + textBox1.Text + "'and Name='" + textBox1.Text + "'and Salary='+textBox3.Text+' ";
SqlCommand MyCommand = new SqlCommand(MyDelete, MyConnection);
try
{
MyConnection.Open();
MyCommand.ExecuteNonQuery();
MyConnection.Close();
}
catch (Exception ex)
{
Console.WriteLine("{0} Exception caught.", ex);
}
}
//更新
private void button3_Click(object sender, EventArgs e)
{
string Name = textBox1.Text;
string Salary = textBox2.Text;
string id = textBox3.Text;
string MyUpdate = "Update Table_1 set Name='" + Name + "',Salary='" + Salary + "' where id='" + textBox3.Text+"'";
SqlCommand MyCommand = new SqlCommand(MyUpdate, MyConnection);
try
{
MyConnection.Open();
MyCommand.ExecuteNonQuery();
MyConnection.Close();
}
catch (Exception ex)
{
Console.WriteLine("{0} Exception caught.", ex);
}
}
//查询数据
private void button4_Click(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection("server=(local);database=Text1;Uid=sa;Pwd=aaaaaa");
cn.Open();
SqlDataAdapter dap = new SqlDataAdapter("SELECT Name,Salary,id FROM Table_1", cn);
DataSet ds = new DataSet();//实例化DataSet类
dap.Fill(ds, "Table");//添加SQL语句并执行
dataGridView1.DataSource = ds.Tables[0].DefaultView;//显示数据
}
相关文章推荐
- C# Oracle、Sql Server连接(增、删、改、查)
- SQL Server 连接
- SQL Server 连接字符串代码
- java 连接 SQL server 数据库时 出现的问题,特别需要注意(包括 版本 区别)
- SQL Server 链接服务器连接 SQLite数据库文件
- JDBC与SQL SERVER 连接问题
- 怎么才能限制SQL Server只能让指定的机器连接
- sql server 2000和sqlserver 2005 连接字符串的区别 IWAS0126E尝试建立连接1遇到问题,原因:No suitable driver
- 从远程连接 SQL Server 需要设置
- 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。 (provider: Nam
- php 连接 SQL Server 2008
- 通过数据链接(Database Link),从Oracle 10g 连接 SQL Server 2000(实例)
- 为 SQL Server 2005 Express Edition 或 SQL Server 2005 Developer Edition 启用远程连接 必须为要从远程计算机连接到的每个 SQL S
- SQL Server 2008开启sa账户以及如何用JDBC进行连接 .
- 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。
- 远程连接SQL Server 2008,服务器端和客户端配置
- SQL Server客户端无法连接服务器的诊断和解决
- python使用pyodbc连接sql server 2008
- 使用JDBC连接SQL SERVER 2008的方法
- SQL SERVER问题解决:已成功与服务器建立连接,但是在登录过程中发生错误。 (provider: 命名管道提供程序, error: 0 - 管道的另一端上无任何进程。)