检索列数据并使用文本框进行检查 [英] retrieving column data and check with textbox

查看:63
本文介绍了检索列数据并使用文本框进行检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在文本框中输入了一个名称,我想检查名称是否已经在数据库中,如果我不想添加名称,或者我想更新名称以及更多详细信息



i希望从我的表中检索一列数据,我想检查列数据是否包含我在文本框中输入的数据



product1字段在产品表中的第零个位置出现。

因此,如果条件我想检查产品是否在product1字段中出现,如果它发生,如果它发生它转到if条件声明它是否去to else声明



Suppose i enter a name in textbox i want to check that the name is already in database or not if it not i want to add the name or i want to update the name with further details

i want to retrieve one column of data from my table and i want to check that column data is contain the data that i enter in my textbox

product1 field occure in zeroth position in product table.
so in if condition i want to check the product is occure in product1 field or not if it occure it goes to if condition statement or not it goes to else statement

string t = "select Product1 from Product";
SqlDataAdapter fd = new SqlDataAdapter(t, DbConnection.mCon);
DataSet ds = new DataSet();
fd.Fill(ds);
if(ds.Tables[0].ToString()==TextBox1.Text)
{
 DateTime date = DateTime.Parse(DateTime.Now.ToShortDateString());
 string dt = date.ToString("MM/dd/yyyy");
 string month = date.ToString("MMMM");
 string vv = "session1";
string s11 = "update Product set Product1='" + TextBox1.Text + "' Quantity='" + TextBox2.Text + "' Price='" + TextBox3.Text + "' Date='" + dt + "' Month='" + month + "' Session='" + vv + "' where Product1='" + TextBox1.Text + "' ";
SqlCommand cmdh = new SqlCommand(s11, DbConnection.mCon);
cmdh.ExecuteNonQuery();
Response.Write("<script>alert('PRODUCT IS UPDATED')</script>");      
}
else
{
 DateTime date = DateTime.Parse(DateTime.Now.ToShortDateString());
 string dt = date.ToString("MM/dd/yyyy");
 string month = date.ToString("MMMM");
 string vv = "session1";
 string s = "insert into Product(Product1,Quantity,Price,Date,Session,Month) values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + dt + "','" + vv + "','" + month + "')";
 cmd = new SqlCommand(s, DbConnection.mCon);
 cmd.ExecuteNonQuery();
 Response.Write("<script>alert('NEW PRODUCT INSERTED')</script>");
 TextBox1.Text = TextBox2.Text = TextBox3.Text = "";
}

推荐答案

most simple way u can do it like->

After 
fd.fill(ds);
DataGridView.dataSource=ds;  //U have a control DataGridView
if(urDataGridViewName.Rows.Count >0)  //means records exist in database SoUpdat
{
//Now apply Update Query
}
else   //Record not in database So Create query will applu
{
//Now Apply Insert Query
}






这里是第一个填充数据库中的数据并检查该值是否与 if语句中的文本框值匹配。而不是这样做你可以像这样优化你的查询:



Hi,

Here you are 1st populating the data from the database and checking whether that value matches with your textbox valus in the if statement. Instead of doing that you canoptimize your query like this:

string t = "select * from Product where Product1 = " + TextBox1.Text;
SqlDataAdapter fd = new SqlDataAdapter(t, DbConnection.mCon);
DataSet ds = new DataSet();
fd.Fill(ds);
if(ds.Tables[0].Rows.Count > 0)
{
 // Your operations that you want to do.      
}
else
{
 // The operations that you want to do here.
}





这将减少您的数据检索所需的时间。



谢谢



This will reduce the time taken for your data retrieval.

Thanks


这篇关于检索列数据并使用文本框进行检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆