C#组合框值传递给SQL参数 [英] C# combobox value to pass to SQL parameter

查看:31
本文介绍了C#组合框值传递给SQL参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题与我先前的问题有关:

从SQL填充C#组合框并根据选择限制结果

希望得到更多的关注.给定一个我想从中传递值的组合框.在SelectedIndexChange属性上,放置了一个方法,该方法从SQL调用存储过程.

  SqlCommand命令;command.Parameters.Add("@ someparameter",combobox1.GetItemText(combobox1.SelectedItem));command.CommandText ="StoredProcedure1";command.ExecuteNonQuery(); 

嗯,它没有任何作用.有人指出我在做什么错/遗漏吗?谢谢

解决方案

您有一个SQL命令和一个参数.您执行了命令,但是建立了什么连接?

就像我启动引擎一样,但实际上我还没有开车的其余部分.我可以打开它,但是没有汽车我就不能开车.

您可以通过执行以下操作来解决此问题:

SqlCommand newCmd = new SqlCommand("SELECT @someParameter from DATABASE",conn);

请注意上述命令末尾的,conn .这使您可以使用诸如 ExecuteNonQuery()之类的方法,并且该命令知道对其查询使用哪个连接.

This question is related to my previous one here:

Populating C# combobox from SQL and restrict results based on choice

hoping to get more attention. Given a combobox from which I'd like to pass the value. On the SelectedIndexChange property I put a method, which calls a Stored Procedure from SQL.

SqlCommand command;

command.Parameters.Add("@someparameter", combobox1.GetItemText(combobox1.SelectedItem));

command.CommandText = "StoredProcedure1";

command.ExecuteNonQuery();

Well, it takes no effect. Anyone to point out what I'm doing wrong/missing? Thank you

解决方案

You have an SQL command and a parameter. You execute the command, but into what connection?

It's like me starting my engine, but I haven't actually got the rest of the car to drive. I can turn it on, but without a car I can't drive.

You can fix this by doing something like this:

SqlCommand newCmd = new SqlCommand("SELECT @someParameter from DATABASE", conn);

Notice the , conn at the end of the above command. This allows you to use a method such as ExecuteNonQuery(), and the command knows which connection to use for its query.

这篇关于C#组合框值传递给SQL参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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