【求助】有一个C#的程序好多地方不对大家看看
帮忙看一下,有好多地方不对帮改改啊!!数据库连接就不用看了using System ;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OracleClient;
using
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
string name = null;
string id = null;
string job = null;
string deptno = null;
//显示信息模块的字段值
String ename=null;
String empnd=null;
String ejob=null;
String emgr=null;
String ehiredate=null;
String esal=null;
String ecomm=null;
String edetnd=null;
//数据库连接字符串
String myurl = null;
OracleConnection orclconn = null;
public Form1()
{
InitializeComponent();
}
private void groupBox2_Enter(object sender, EventArgs e)
{
}
//插入图片
private void picture( )
{
try
{
getConnection();
orclconn.Open();
}
catch
{ }
OracleCommand mycommand = orclconn.CreateCommand();
= "insert into aa(image) values (:zp)";
//MessageBox.Show();
mycommand.ExecuteNonQuery();
string imgPath = @"G:\体育\科比的照片\科比.gif ";//图片文件所在路径
FileStream file = new FileStream(imgPath, FileMode.Open, FileAccess.Read);
Byte[] imgByte = new Byte[file.Length];//把图片转成 Byte型 二进制流
file.Read(imgByte, 0, imgByte.Length);//把二进制流读入缓冲区
file.Close();
//mycommand.Parameters.Add("zp", System.Data.OleDb.OleDbType.Binary, imgByte.Length);
//cmd.Parameters[0].Value = imgByte;
try
{
mycommand.ExecuteNonQuery();
MessageBox.Show("插入成功");
}
catch (System.Exception e1)
{
MessageBox.Show(e1.Message);
}
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“dataSet1.EMP”中。您可以根据需要移动或删除它。
this.eMPTableAdapter.Fill(this.dataSet1.EMP);
// TODO: 这行代码将数据加载到表“dataSet1.EMP”中。您可以根据需要移动或删除它。
// this.eMPTableAdapter.Fill(this.dataSet1.EMP);
}
private void getConnection()
{
//数据库连接方法
myurl = "Data Source=orcl;User ID=scott;Password=orcl;Integrated security=false";//连接字符串
orclconn = new OracleConnection(myurl);//创建连接
}
private void sql_select()
{
}
private void sql_update()
{
try
{
empnd = textBox3.Text;
ename = textBox8.Text;
ejob = textBox5.Text;
emgr = textBox9.Text;
ehiredate = textBox6.Text;
edetnd = textBox11.Text;
ecomm = textBox7.Text;
esal = textBox10.Text;
getConnection();//连接数据库
orclconn.Open();
OracleCommand mycommand = orclconn.CreateCommand();
= "update emp set ename='" + ename + "', job='" + ejob + "', mgr='" + emgr + "', hiredate=to_date('" + ehiredate + "','yyyy-mm-dd hh24:mi:ss'), sal='" + esal + "', comm='" + ecomm + "', deptno='" + edetnd + "'where empno='" + empnd + "'";
MessageBox.Show();
mycommand.ExecuteNonQuery();
orclconn.Close();
}
catch (Exception eq)
{
MessageBox.Show("有错误!!");
}
}
private void sql_delete()
{
try
{
//添加新纪录
empnd = textBox3.Text;
getConnection();//连接数据库
orclconn.Open();
OracleCommand mycommand = orclconn.CreateCommand();
= "delete from emp where empno='"+empnd+"'";
mycommand.ExecuteNonQuery();
orclconn.Close();
// MessageBox.Show("dddddddddddd");
}
catch (Exception el)
{
MessageBox.Show("有错误!!");
}
}
private void sql_insert()
{
try
{
//添加新纪录
empnd = textBox3.Text;
ename = textBox8.Text;
ejob = textBox5.Text;
emgr = textBox9.Text;
ehiredate = textBox6.Text;
edetnd = textBox11.Text;
ecomm = textBox7.Text;
esal = textBox10.Text;
getConnection();//连接数据库
orclconn.Open();
OracleCommand mycommand = orclconn.CreateCommand();
= "insert into emp values('" + empnd + "','" + ename + "','" + ejob + "','" + emgr + "','" + ehiredate + "','" + esal + "','" + ecomm + "','" + edetnd + "')";
mycommand.ExecuteNonQuery();
orclconn.Close();
// MessageBox.Show("dddddddddddd");
}
catch(Exception el)
{
MessageBox.Show("有错误!!");
}
}
private void button1_Click(object sender, EventArgs e)
{
//普通查询
try
{
String sql = "select * from emp where ";
name = textBox1.Text;
id = textBox2.Text;
if(comboBox1.Text!="")
job = comboBox1.SelectedItem.ToString();
if (comboBox1.Text != "")
deptno = comboBox1.SelectedItem.ToString();
if (name == "" && id == "" && job == null && deptno == null)
MessageBox.Show("请输入查询条件");
else
{
getConnection();//连接数据库
if (name != "")
sql = String.Concat(sql, "ename='" + name + "'and ");
if (id != "")
sql = String.Concat(sql, "empnd='" + id + "'and ");
if (job != null)
sql = String.Concat(sql, "job='" + job + "'and ");
if (deptno != null)
sql = String.Concat(sql, "edetnd='" + deptno + "'");
else
sql = sql.Remove(sql.Length - 4);
orclconn.Open();
MessageBox.Show(sql);
// OracleTransaction mytansaction = orclconn.BeginTransaction();//开始事务
// OracleCommand mycommand = orclconn.CreateCommand();//创建sql命令变量
DataTable table = new DataTable();
OracleDataAdapter myadapter = new OracleDataAdapter(sql, orclconn);
myadapter.Fill(table);
dataGridView1.DataSource = table;
// ();//提交事务
orclconn.Close();//关闭数据库连接
}
}
catch (Exception ew)
{
MessageBox.Show("有错误!!");
}
}
private void button6_Click(object sender, EventArgs e)
{
//语句查询
try
{
String input_sql = textBox4.Text;
if (input_sql != "")
{
getConnection();//创建连接
orclconn.Open();
DataTable table2 = new DataTable();
OracleDataAdapter myadapter = new OracleDataAdapter(input_sql, orclconn);
myadapter.Fill(table2);
dataGridView1.DataSource = table2;
orclconn.Close();//关闭数据库连接
// MessageBox.Show(input_sql);
}
else
{ MessageBox.Show("请输入SQL语句!"); }
}
catch (Exception ew)
{
MessageBox.Show("输入有错误!!");
}
}
private void displayInfo()
{
//int i = dataGridView1.CurrentCell.RowIndex;
empnd = dataGridView1.CurrentRow.Cells[0].Value.ToString();
ename = dataGridView1.CurrentRow.Cells[1].Value.ToString();
ejob = dataGridView1.CurrentRow.Cells[2].Value.ToString();
emgr = dataGridView1.CurrentRow.Cells[3].Value.ToString();
ehiredate = dataGridView1.CurrentRow.Cells[4].Value.ToString();
esal = dataGridView1.CurrentRow.Cells[5].Value.ToString();
ecomm = dataGridView1.CurrentRow.Cells[6].Value.ToString();
edetnd = dataGridView1.CurrentRow.Cells[7].Value.ToString();
textBox8.Text=ename;
textBox3.Text=empnd;
textBox5.Text=ejob;
textBox9.Text=emgr;
textBox6.Text= ehiredate;
textBox11.Text=edetnd;
textBox7.Text=ecomm;
textBox10.Text=esal;
// MessageBox.Show(i+ename);
}
private void button2_Click(object sender, EventArgs e)
{
//添加新行
sql_insert();
}
private void haha(object sender, DataGridViewCellEventArgs e)
{
//单击表单事件
displayInfo();
}
private void button3_Click(object sender, EventArgs e)
{
//修改
sql_update();
}
private void button4_Click(object sender, EventArgs e)
{
//删除
sql_delete();
}
private void button5_Click(object sender, EventArgs e)
{
//取消
DialogResult result= MessageBox.Show("确定退出?","请确认",MessageBoxButtons.YesNo,MessageBoxIcon.Question,MessageBoxDefaultButton.Button2);
if (result == DialogResult.Yes)
Application.Exit();
else
return;
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
}
private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
}
}
}