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.OleDb;
using System.Reflection;
using System.Xml;
using System.Data.SqlClient;namespace charushuju1
{
public partial class Form1 : Form
{
string connsqlserver = "Data Source=localhost;Initial Catalog=Lee;Integrated Security=True";
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
try
{
if (TextBox1.Text == "" || TextBox2.Text == "" || TextBox3.Text == "" || TextBox4.Text == "" || TextBox5.Text == "" || TextBox6.Text == "")
{
MessageBox.Show("请输入完整的联系人信息", "提示信息",
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Warning);
}
else
{
DataSet ds = new DataSet();
string strSqls;
strSqls = string.Format("SELECT * FROM ziliao WHERE ID='" + TextBox1.Text.Trim() + "'");
SqlConnection con = new SqlConnection(consqlserver);
SqlDataAdpter da = new SqlDataAdpaer(strSqls, con);
// SqlConnection cons = new SqlConnection(consqlserver);
// SqlCommand cmd = new SqlCommand(strSqls, cons); da.Fill(ds);
if (ds.Tables[0].Rows.Count > 1)
{
MessageBox.Show("该人ID已经存在,请您更换此人ID", "提示信息",
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Error); }
else
{
if (MessageBox.Show("确认要保存该人信息吗?", "提示信息",
System.Windows.Forms.MessageBoxButtons.YesNo,
System.Windows.Forms.MessageBoxIcon.Question) ==
System.Windows.Forms.DialogResult.Yes)
{
strSqls = string.Format(@"INSERT INTO Lee (ID,Name,Age,Sex,xuexiao,zhuanye) VALUES('{0}',{1},{2},{3},{4},{5})", TextBox1.Text.Trim(),
TextBox2.Text.Trim(), TextBox3.Text.Trim(), TextBox4.Text.Trim(), TextBox5.Text.Trim(), TextBox6.Text.Trim());
SqlConnection cons = new SqlConnection(consqlserver);
SqlCommand cmd = new SqlCommand(strSqls, cons);
try
{
cons.Open(); //打开连接
cmd.ExecuteNonQuery(); //执行SQL语句
}
catch { }
finally
{
cons.Close(); //关闭连接
cons.Dispose(); //释放连接
cmd.Dispose();
}
MessageBox.Show("信息保存成功", "提示信息",
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Information);
string sqls = "select * from Lee ORDER BY ID";
SqlDataAdapter = new SqlDataAdapter(sqls, con);
da1.Fill(ds);
if (ds.Tables[0].Rows.Count > 1)
{
dataGridView1.DataSource = ds.Tables[0];
} } }
} }
catch { }
} private void button2_Click(object sender, EventArgs e)
{
this.Close();
} private void Form1_Load(object sender, EventArgs e)
{
DataSet ds = new DataSet();
//定义连接数据源
string sql = "select * from ziliao";
//定义SQL Server连接对象
SqlConnection con = new SqlConnection(consqlserver);
SqlDataAdapter da = new SqlDataAdapter(sql, con); try
{
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
//将获得数据源赋予数据库控件
dataGridView1.DataSource = ds.Tables[0];
} }
catch (Exception ex)
{
throw new Exception(ex.ToString());
}
finally
{
con.Close();
con.Dispose();
da.Dispose();
}
}
}
}
提示有一个错误:错误 1 “charushuju.Form1.Dispose(bool)”: 没有找到适合的方法来重写 E:\vs2008例子\charushuju1\charushuju1\Form1.Designer.cs 14 33 charushuju1
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.OleDb;
using System.Reflection;
using System.Xml;
using System.Data.SqlClient;namespace charushuju1
{
public partial class Form1 : Form
{
string connsqlserver = "Data Source=localhost;Initial Catalog=Lee;Integrated Security=True";
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
try
{
if (TextBox1.Text == "" || TextBox2.Text == "" || TextBox3.Text == "" || TextBox4.Text == "" || TextBox5.Text == "" || TextBox6.Text == "")
{
MessageBox.Show("请输入完整的联系人信息", "提示信息",
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Warning);
}
else
{
DataSet ds = new DataSet();
string strSqls;
strSqls = string.Format("SELECT * FROM ziliao WHERE ID='" + TextBox1.Text.Trim() + "'");
SqlConnection con = new SqlConnection(consqlserver);
SqlDataAdpter da = new SqlDataAdpaer(strSqls, con);
// SqlConnection cons = new SqlConnection(consqlserver);
// SqlCommand cmd = new SqlCommand(strSqls, cons); da.Fill(ds);
if (ds.Tables[0].Rows.Count > 1)
{
MessageBox.Show("该人ID已经存在,请您更换此人ID", "提示信息",
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Error); }
else
{
if (MessageBox.Show("确认要保存该人信息吗?", "提示信息",
System.Windows.Forms.MessageBoxButtons.YesNo,
System.Windows.Forms.MessageBoxIcon.Question) ==
System.Windows.Forms.DialogResult.Yes)
{
strSqls = string.Format(@"INSERT INTO Lee (ID,Name,Age,Sex,xuexiao,zhuanye) VALUES('{0}',{1},{2},{3},{4},{5})", TextBox1.Text.Trim(),
TextBox2.Text.Trim(), TextBox3.Text.Trim(), TextBox4.Text.Trim(), TextBox5.Text.Trim(), TextBox6.Text.Trim());
SqlConnection cons = new SqlConnection(consqlserver);
SqlCommand cmd = new SqlCommand(strSqls, cons);
try
{
cons.Open(); //打开连接
cmd.ExecuteNonQuery(); //执行SQL语句
}
catch { }
finally
{
cons.Close(); //关闭连接
cons.Dispose(); //释放连接
cmd.Dispose();
}
MessageBox.Show("信息保存成功", "提示信息",
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Information);
string sqls = "select * from Lee ORDER BY ID";
SqlDataAdapter = new SqlDataAdapter(sqls, con);
da1.Fill(ds);
if (ds.Tables[0].Rows.Count > 1)
{
dataGridView1.DataSource = ds.Tables[0];
} } }
} }
catch { }
} private void button2_Click(object sender, EventArgs e)
{
this.Close();
} private void Form1_Load(object sender, EventArgs e)
{
DataSet ds = new DataSet();
//定义连接数据源
string sql = "select * from ziliao";
//定义SQL Server连接对象
SqlConnection con = new SqlConnection(consqlserver);
SqlDataAdapter da = new SqlDataAdapter(sql, con); try
{
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
//将获得数据源赋予数据库控件
dataGridView1.DataSource = ds.Tables[0];
} }
catch (Exception ex)
{
throw new Exception(ex.ToString());
}
finally
{
con.Close();
con.Dispose();
da.Dispose();
}
}
}
}
提示有一个错误:错误 1 “charushuju.Form1.Dispose(bool)”: 没有找到适合的方法来重写 E:\vs2008例子\charushuju1\charushuju1\Form1.Designer.cs 14 33 charushuju1
解决方案 »
- 带母版页的页面上,有UPDATEPANEL,其中的TEXTBOX如何设置焦点?
- asp.net如何将代码发布到远程服务器上
- div+html 动态图片的问题
- asp.net 过滤字符串中的<a>标签
- 怎么控制前台的显示
- 问一个在页面上浏览附件的问题,有关文件上传下载和urlencode,请帮忙!--50分
- 整页缓存outputcache后,如果让里面的局部不缓存呢?
- 【菜鸟求助】VS2012设置默认的调试浏览器问题
- ie可以访问http://localhost/iisstart.htm,chrome不可以
- 有没有从ASP转过来的,有问题要问?---- 再问一次各位?
- DataFormatString="{0:yyyy年MM月dd日}"如何添加显示星期几
- Jquery的问题
da.Dispose();
去掉