关键词搜索

源码搜索 ×
×

C# Oracle、Sql Server连接(增、删、改、查)

发布2014-01-08浏览3036次

详情内容

1、连接oracle,并可以将数据库的数据显示在 gridControl上

  1. private void Btn_XS_Click(object sender, EventArgs e)
  2. {
  3. //写连接串
  4. //ntegrated Security 身份验证方式
  5. //当为false时,将在连接中指定用户ID和密码。
  6. //当为true时,将使用当前的Windows帐户凭据进行身份验证。
  7. //可识别的值为true、false、yes、no以及与true等效的sspi。
  8. string ConnectionString = "Data Source=数据库名;User Id=用户名;Password=密码;Integrated Security=no;";
  9. //创建一个新连接
  10. OracleConnection conn=new OracleConnection(ConnectionString);
  11. //以上两句也可以写成 OracleConnection conn=new OracleConnection "Data Source=数据库名;User Id=用户名;Password=密码;Integrated Security=no;");
  12. try
  13. {
  14. conn.Open();
  15. //下面这句话,即使是select....返回一个int类型的数,也要按下面这么利用数据集去做
  16. //不可能用一句话来实现
  17. //注意从函数外面传递参数到sql语句中的写法
  18. //比如传递AdNumber
  19. //"selectyhbh from gspuser where yhbh='" + AdNumber + "'"
  20. OracleCommand cmd = new OracleCommand("select * from FY", conn);
  21. OracleDataAdapter oda = new OracleDataAdapter();
  22. oda.SelectCommand = cmd;
  23. DataSet ds = new DataSet();
  24. oda.Fill(ds);
  25. //如果这想要第一行第一列可以这么写ds.Tables[0].Rows[0][0]
  26. gridControl1.DataSource = ds.Tables[0].DefaultView;
  27. conn.Close();
  28. }
  29. catch (Exception ee)
  30. {
  31. //如果有错误,输出错误信息
  32. MessageBox.Show(ee.Message);
  33. }
  34. finally
  35. {
  36. //关闭连接
  37. conn.Close();
  38. }
  39. }
  1. //修改
  2. string ConnectionString = "DataSource=ORCL;User Id=system;Password=aaaaaa;Integrated Security=no;";
  3. OracleConnection conn=new OracleConnection(ConnectionString);
  4. conn.Open();
  5. string str1 = "SQL修改语句'";
  6. //当不传递conn时,会提示连接没有打开
  7. OracleCommand cmd1 = new OracleCommand(str1,conn);
  8. //ExecuteNonQuery()对于Update,Insert,Delete 语句执行成功是返回值为该命令所影响的行数
  9. int result=cmd1.ExecuteNonQuery();
2、sql server连接,并实现增、删、改、查

  1. static string MyConn = "server=127.0.0.1;uid=sa;pwd=密码;database=Text1;Trusted_Connection=no";
  2. SqlConnection MyConnection = new SqlConnection(MyConn);
  3. //此处的表名Table_1,数据库名Text1,表中一共有3列Name, Salary,id
  4. //增加
  5. private void button1_Click(object sender, EventArgs e)
  6. { //输入框
  7. string MyInsert = "insert into Table_1(Name, Salary,id)values('" + Convert.ToString(textBox1.Text) + "','" + Convert.ToString(textBox2.Text) + "','" + Convert.ToString(textBox3.Text)+ "')";
  8. SqlCommand MyCommand = new SqlCommand(MyInsert, MyConnection);
  9. try
  10. {
  11. MyConnection.Open();
  12. MyCommand.ExecuteNonQuery();
  13. MyConnection.Close();
  14. }
  15. catch (Exception ex)
  16. {
  17. Console.WriteLine("{0} Exception caught.", ex);
  18. }
  19. }
  20. //删除
  21. private void button2_Click(object sender, EventArgs e)
  22. {
  23. string MyDelete = "Delete from Table_1 where id='" + textBox3.Text + "'";
  24. //string MyDelete = "Delete from Table_1 where id='" + textBox1.Text + "'and Name='" + textBox1.Text + "'and Salary='+textBox3.Text+' ";
  25. SqlCommand MyCommand = new SqlCommand(MyDelete, MyConnection);
  26. try
  27. {
  28. MyConnection.Open();
  29. MyCommand.ExecuteNonQuery();
  30. MyConnection.Close();
  31. }
  32. catch (Exception ex)
  33. {
  34. Console.WriteLine("{0} Exception caught.", ex);
  35. }
  36. }
  37. //更新
  38. private void button3_Click(object sender, EventArgs e)
  39. {
  40. string Name = textBox1.Text;
  41. string Salary = textBox2.Text;
  42. string id = textBox3.Text;
  43. string MyUpdate = "Update Table_1 set Name='" + Name + "',Salary='" + Salary + "' where id='" + textBox3.Text+"'";
  44. SqlCommand MyCommand = new SqlCommand(MyUpdate, MyConnection);
  45. try
  46. {
  47. MyConnection.Open();
  48. MyCommand.ExecuteNonQuery();
  49. MyConnection.Close();
  50. }
  51. catch (Exception ex)
  52. {
  53. Console.WriteLine("{0} Exception caught.", ex);
  54. }
  55. }
  56. //查询数据
  57. private void button4_Click(object sender, EventArgs e)
  58. {
  59. SqlConnection cn = new SqlConnection("server=(local);database=Text1;Uid=sa;Pwd=aaaaaa");
  60. cn.Open();
  61. SqlDataAdapter dap = new SqlDataAdapter("SELECT Name,Salary,id FROM Table_1", cn);
  62. DataSet ds = new DataSet();//实例化DataSet类
  63. dap.Fill(ds, "Table");//添加SQL语句并执行
  64. dataGridView1.DataSource = ds.Tables[0].DefaultView;//显示数据
  65. }




相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载