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

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(); 
              }
        } 
//修改
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;//显示数据
        }




上一篇:bilibili 实时平台的架构与实践


下一篇:福利!阿里巴巴研发效能峰会直播预约倒计时