在C#中的SQL Server数据库中检索数据 [英] Retrieve data from a SQL Server database in C#

查看:116
本文介绍了在C#中的SQL Server数据库中检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3列和数据库表年龄。在我的C#Windows应用程序,我有一个名为文本框3 TextBox1中 ...我用这个code使我的连接我的SQL Server:

I have a database table with 3 columns firstname, Lastname and age. In my C# Windows application I have 3 textboxes called textbox1... I made my connectivity to my SQL Server using this code:

SqlConnection con = new SqlConnection("Data Source = .;
                                       Initial Catalog = domain;
                                       Integrated Security = True");
con.Open();
SqlCommand cmd = new SqlCommand("Select * from tablename", con);

我想从我的数据库值;如果我给在 TextBox1中 A值,它必须匹配数据库中的值和检索等细节到相应的文本框。

I'd like to get values from my database; if I give a value in textbox1 it has to match the values in the database and retrieve other details to the corresponding textboxes.

这个方法我试过,但它不工作:

I tried this method but it's not working:

cmd.CommandText = "select * from tablename where firstname = '" + textBox1.Text + "' ";

我该怎么办呢检索所有其他值文本框?

How can I do it to retrieve all the other values to the textboxes?

推荐答案

几点需要注意这里:我用一个参数化查询,使您的code更安全。你正在使用select语句的方式,其中x =+ Textbox.Text +部分打开您SQL注入攻击。

public Person SomeMethod(string fName) { var con = ConfigurationManager.ConnectionStrings["Yourconnection"].ToString(); Person matchingPerson = new Person(); using (SqlConnection myConnection = new SqlConnection(con)) { string oString = "Select * from Employees where FirstName=@fName"; SqlCommand oCmd = new SqlCommand(oString, myConnection); oCmd.Parameters.AddWithValue("@Fname", fName); myConnection.Open(); using (SqlDataReader oReader = oCmd.ExecuteReader()) { while (oReader.Read()) { matchingPerson.firstName = oReader["FirstName"].ToString(); matchingPerson.lastName = oReader["LastName"].ToString(); } myConnection.Close(); } } return matchingPerson; }

我已经改变了这:

I've changed this to:

那么,code这个块要做的是:

"Select * from Employees where FirstName=@fName" oCmd.Parameters.AddWithValue("@Fname", fName);

执行SQL语句对数据库,看看是否有有匹配您提供的之一的任何firstnames。
如果是这样的情况下,该人将被存储在一个人对象(见下文在我的回答为类)。
如果没有比赛,该Person对象的属性将

So what this block of code is going to do is:

很显然,我并不确切地知道你正在尝试做的,所以那里有几件事情要注意:当有更多然后1人具有匹配名称,只有最后一个将被保存并返回给你。
如果您希望能够存储这些数据,你可以将它们添加到列表与LT;人方式>

Execute an SQL statement against your database, to see if any there are any firstnames matching the one you provided. If that is the case, that person will be stored in a Person object (see below in my answer for the class). If there is no match, the properties of the Person object will be null.

Person类,使它更清洁:

Obviously i don't exactly know what you are trying to do, so theres a few things to pay attention to: when there are more then 1 persons with a matching name, only the last one will be saved and returned to you. If you want to be able to store this data, you can add them to a List<Person> .

Person class to make it cleaner:

现在要调用的方法:

Now to call the method:

您可以再与值从Person对象来像这样填满你的文本框

You can then fill your textboxes with values coming from the Person object like so:

txtLastName.Text = x.LastName;

这篇关于在C#中的SQL Server数据库中检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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