C# 如何使用SQL表中的数据验证用户在DataGridView中输入的值?
我是C#and Grid的新手, 我在SQL中有一个表,它包含两列,detaciets和Price,每边都有几个值。另一方面,在C#win.form网格上,有两列detacies和grid。当用户在第1列(详情)中输入任何数据时,如果输入的数据与表Special的值中的值不匹配,则应抛出异常。 为此,我使用了CellEndEdit事件,但在用户在空单元格中输入数据后,如何根据DB表值检查输入的数据是否正确 我已成功地将表单与DB连接,并使用VS的“数据设置”选项成功地完成了此操作,但不知道如何使用SQL数据库进行连接,我已尝试过,但在使用SQL数据库进行验证时感到困惑。以下是我的代码:C# 如何使用SQL表中的数据验证用户在DataGridView中输入的值?,c#,winforms,C#,Winforms,我是C#and Grid的新手, 我在SQL中有一个表,它包含两列,detaciets和Price,每边都有几个值。另一方面,在C#win.form网格上,有两列detacies和grid。当用户在第1列(详情)中输入任何数据时,如果输入的数据与表Special的值中的值不匹配,则应抛出异常。 为此,我使用了CellEndEdit事件,但在用户在空单元格中输入数据后,如何根据DB表值检查输入的数据是否正确 我已成功地将表单与DB连接,并使用VS的“数据设置”选项成功地完成了此操作,但不知道如何使
namespace Small_Billing_System
{
public partial class hitbill : Form
{
SqlConnection cn = new SqlConnection(@"server=people-pc\sqlexpress;integrated security=true;database=nec");
SqlCommand cm = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
//da1, da2;
// ds1, ds2;
DataSet ds = new DataSet();
SqlCommandBuilder cb = new SqlCommandBuilder();
public hitbill()
{
InitializeComponent();
}
private void control()
{
//dataGridView_2.DataSource = ds;
//dataGridView_2.DataMember = "tblfood";
//totalbox.DataBindings.Add("Text", ds, "tblfood.Price");
}
private void hitbill_Load(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection(@"server=people-pc\sqlexpress;integrated security=true;database=nec");
try
{
cn.Open();
MessageBox.Show("Data base connected");
}
catch (Exception)
{
MessageBox.Show("Data base connection failed");
throw;
}
cn.Open();
cm = new SqlCommand("select * from tblfood", cn);
da = new SqlDataAdapter(cm);
da.Fill(ds, "tblfood");
}
private void dataGridView_2_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
DataGridViewCell currCell = dataGridView_2.CurrentCell;
string currCellContent = currCell.Value.ToString();
// SqlCommand cmd = new SqlCommand("select * FROM tblfood");
//check whether inputted values are in DB or not
if (dataGridView_2.CurrentCell.ColumnIndex == 0)
{
// if (!((currCellContent == ???????????????.
// if (!((currCellContent == "Beans") || (currCellContent == "Juice"))) //cannot do this because there might be many particulars not just 2 or 3
//And when there are many particulars every time when user inputs value it should be checked with database and throw an exception/error in case of wrong input.
// {
// MessageBox.Show("Paticulars not found");
// dataGridView_2.CurrentCell.Value = "";
// }
// }
// else if (dataGridView_2.CurrentCell.ColumnIndex ==1)
// {
// double R = 0.00;
// string particular = dataGridView_2.CurrentRow.Cells[0].Value.ToString().Trim();
// if (particular == "Beans")
// {
// R = Double.Parse(currCellContent) * Properties.Data1.Default.Apple;
// }
// else if (particular == "Juice")
// {
// R = Double.Parse(currCellContent) * Properties.Data1.Default.Orange;
// }
// else
// {
// R = 0.00;
// }
// dataGridView_2.CurrentRow.Cells[2].Value = R.ToString();
// DataGridViewRowCollection rows = dataGridView_2.Rows;
// double total = 0.00;
// foreach (DataGridViewRow row in rows)
// {
// if (!row.IsNewRow)
// {
// double price = Double.Parse(row.Cells[2].Value.ToString());
// total = total + price;
// }
// }
// totalbox.Text = total.ToString();
// }
//}
}
}
}
}
我希望我读对了 那么,您有一个SQL中充满数据的表?“详情”和“价格”两栏 您有一个带有Datagridview的Winform,它具有相同的列吗 您还将其连接起来以便执行查询 我这样想对吗?如果是这样的话,那就这样吧 有几种方法可以做到这一点,简单快捷的方法就是通过SQL进行检查 因此,创建一个简单的Select存储过程
CREATE PROCEDURE [dbo].[ValidationResult]
@particular as varchar(50)
AS
SELECT Particulars, Price FROM Yourtable
WHERE Particulars = @particular
因此,现在只需设置链接即可进行简单的快速查询
public DataTable SQL_ValidateCheck(string part)
{
try
{
DataTable tbl_val = new DataTable();
using (SqlConnection AutoConn = new SqlConnection(conn32))
{
AutoConn.Open();
using (SqlCommand InfoCommand = new SqlCommand())
{
using (SqlDataAdapter infoAdapter = new SqlDataAdapter(InfoCommand))
{
InfoCommand.Connection = AutoConn;
InfoCommand.CommandType = CommandType.StoredProcedure;
InfoCommand.CommandText = "dbo.ValidationResults";
InfoCommand.Parameters.AddWithValue("@particular", part);
InfoCommand.CommandTimeout = 180;
infoAdapter.Fill(tbl_val );
AutoConn.Close();
return tbl_val ;
}
}
}
}
catch (Exception e)
{
//MessageBox.Show("Error in connection :: " + e);
return tbl_val ;
}
}
现在您有了一个包含验证选择查询结果的表
你需要做的最后一件事就是使用它
private void Check_Val()
{
DataTable myTable = new DataTable();
myTable = SQL_ValidateCheck(mygrid.CurrentSelection.Cells[0].Value.ToString());
//Iterate the table for the result, maybe use an if(mytable == null) or something
}
可能在网格事件中使用此方法,完全由您决定。
语法可能不正确,最近一直在与Telerik合作,因此请检查语法,但您应该了解其中的含义 谢谢Steven,这是一个对我来说回答得很好的问题,答案和良好的回答将始终激励像我这样的新手程序员。没有问题,如果您觉得有帮助,请确保单击“接受答案”: