SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=PHOEBE-PC;Initial Catalog=Mr_Liu_cs_design;Integrated Security=True";
SqlCommand cmd = new SqlCommand();
cmd.CommandText = string.Format("select COUNT(8)from Mr_Liu_class2_type where t_type='{0}';", temp);
cmd.Connection = con;
con.Open(); //打开数据库连接
int count = Convert.ToInt32(cmd.ExecuteScalar());
if (count > 0)
{
MessageBox.Show("添加计量单位已存在");
}
else
{
cmd.CommandText = string.Format("insert into Mr_Liu_class2_type('{0}')", temp);
cmd.ExecuteNonQuery();//用ExecuteNonQuery()的方法来执行查询语句
MessageBox.Show("添加成功");
}
con.Close(); //关闭数据库的连接
con.ConnectionString = "Data Source=PHOEBE-PC;Initial Catalog=Mr_Liu_cs_design;Integrated Security=True";
SqlCommand cmd = new SqlCommand();
cmd.CommandText = string.Format("select COUNT(8)from Mr_Liu_class2_type where t_type='{0}';", temp);
cmd.Connection = con;
con.Open(); //打开数据库连接
int count = Convert.ToInt32(cmd.ExecuteScalar());
if (count > 0)
{
MessageBox.Show("添加计量单位已存在");
}
else
{
cmd.CommandText = string.Format("insert into Mr_Liu_class2_type('{0}')", temp);
cmd.ExecuteNonQuery();//用ExecuteNonQuery()的方法来执行查询语句
MessageBox.Show("添加成功");
}
con.Close(); //关闭数据库的连接
解决方案 »
- 请问如何将long转成blob?
- 已知一个datatable dt ,乍么去把数据update到结构相同的数据表?
- 求高手,A项目引用B项目的DLL文件,为什么没有代码的自动提示?
- .net 全站静态页面??
- SqlDataReader 问题
- 求救!小组作业未完成."怎么实现表的部分字段添加到新表里?"
- Rss Bandit种子不支持中文
- 这样的面试还有希望吗?
- Winform多线程编程,在辅助线程中Control.Invoke(),为什么窗体中的控件不能获得焦点??
- 班门斧及各路高手请进!关于在应用程序中引用组件后的问题
- 有谁用过GetClassName这个API函数(C#中的字符串指针该如何处理)
- 请各位大神帮忙,程序一运行的时候就卡死
cmd.CommandText = string.Format("select COUNT(8)from Mr_Liu_class2_type where t_type='{0}';", temp); cmd.CommandText = string.Format("insert into Mr_Liu_class2_type('{0}')", temp);这两个temp可以共用??
COUNT(8) from 聚合函数和FROM中间 加个空格试试
string temp;
temp = this.textBox1.Text;
用来装box的字符串的。
string c = "Data Source=PHOEBE-PC;Initial Catalog=Mr_Liu_cs_design;Integrated Security=True";
using(SqlConnection con = new SqlConnection(c)
)
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = string.Format("select COUNT(8)from Mr_Liu_class2_type where t_type='{0}';", temp);
cmd.Connection = con;
con.Open(); //打开数据库连接
int count = Convert.ToInt32(cmd.ExecuteScalar());
if (count > 0)
{
MessageBox.Show("添加计量单位已存在");
}
else
{
cmd.CommandText = string.Format("insert into Mr_Liu_class2_type('{0}')", temp);
cmd.ExecuteNonQuery();//用ExecuteNonQuery()的方法来执行查询语句
MessageBox.Show("添加成功");
}
}楼主代码这样改下