用于在指定数据库中插入值的函数 [英] function to insert values in the specified database
本文介绍了用于在指定数据库中插入值的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想编写一个函数来在指定的数据库中插入一个值。但是如果重复输入的值(值在文本框中),则不应该存储它,并且应该显示一条消息。请帮助我具有此功能的结构......
提前感谢.. :)
I want to write a function to insert a value in the specified database.But if the entered value(the value is in a textbox) is repeated then it should not b stored and a message should be displayed.kindly help me out with the structure of this function...
thanks in advance..:)
推荐答案
private DataSet GetDataByName(string Name)
{
SqlConnection conn =new SqlConnection ( "Data Source=VU-PC\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True");
conn.Open();
SqlCommand comm =new SqlCommand( "Select * from Table where Name = " + Name,conn);
SqlDataAdapter myAdapter = new SqlDataAdapter();
myAdapter.SelectCommand = comm ;
DataSet myDataSet = new DataSet();
myAdapter.Fiill(myDataSet);
conn.Close();
return myDataSet
}
private void InsertInDB()
{
if(GetDataByName(this.txtName.Text).Tables[0].Rows.Count == 0)
{
SqlConnection conn =new SqlConnection ( "Data Source=VU-PC\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True");
conn.Open();
SqlCommand comm =new SqlCommand( "INSERT INTO connect_com "+"(name,E_mail,School) "+" VALUES("+txtName.Text+"','"+"','"+txtEmail.Text+"','"+txtSchool.Text+")",conn);
}
comm.ExecuteNonQuery();
conn.Close();
}
应该对你有帮助吗?
should it help you?
这篇关于用于在指定数据库中插入值的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文