我创建了一个返回表的SQL查询:
家庭|问题
一家人| 11
家庭b | 5
家庭c | 17个
家庭d | 28个
我在Visual Basic(visual studio 2015)中使用此代码将此数据返回到数据表:
using System;
using System.Data.OleDb;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Security.Cryptography;
namespace login
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
OleDbConnection con = new OleDbConnection();
string dbprovider;
string dbsource;
DataSet ds = new DataSet();
string username = "";
string password = "";
private void textBox1_TextChanged(object sender, EventArgs e)
{
username = textBox1.Text;
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
tabControl1.TabPages.Remove(tabPage2);
// TODO: This line of code loads data into the 'login_data1DataSet.Table1' table. You can move, or remove it, as needed.
this.table1TableAdapter.Fill(this.login_data1DataSet.Table1);
}
private void button1_Click(object sender, EventArgs e)
{
dbprovider = "Provider=Microsoft.ACE.OLEDB.12.0;"; //If you’re an avid fan of Google you’ll see this line written in a number of different ways, version 12(this one) is suitable for our version of access.
dbsource = "Data Source = ../login data1.accdb"; //Change this for the location and name of your db.
con.ConnectionString = dbprovider + dbsource;
con.Open(); //If you get this far you’re db is now opened successfully.
OleDbCommand command = new OleDbCommand("SELECT username FROM table1 WHERE username = @username", con);
command.Parameters.AddWithValue("@username", textBox1.Text);
OleDbDataReader reader = command.ExecuteReader();
if (!reader.HasRows)
{
MessageBox.Show("your username was not right");
reader.Close();
command.Dispose();
}
else
{
username = textBox1.Text;
reader.Close();
OleDbCommand command2 = new OleDbCommand("SELECT password FROM table1 WHERE password = @password", con);
command2.Parameters.AddWithValue("@password", textBox2.Text);
command2.Parameters.AddWithValue("@username", textBox1.Text);
OleDbDataReader reader2 = command2.ExecuteReader();
if (!reader2.HasRows)
{
// if (reader2.ToString != textBox2.Text)
MessageBox.Show("your password was not right");
}
else
{
MessageBox.Show("logged in");
tabControl1.TabPages.Insert(1,tabPage2);
password = textBox2.Text;
}
reader2.Close();
command2.Dispose();
}
con.Close();
}
}
}
*这里是我丢失的地方*
我需要代码根据第1列(家庭)中的值选择问题数量(第2列)
我的问题是,有些时候,其中一个家庭可能没有记录,所以数据集可能只有家庭a,c和d ......下次它可能有家庭b和d。所以我不能真正使用行(#)。我需要能够引用姓氏的代码,然后返回问题的数量,将该数字放入字符串中,然后将该值放在文本框中。
我很高兴将字符串分配给变量然后进入文本框......它介于我之间,我无法弄明白!
谢谢!
答案 0 :(得分:2)
我建议使用SqlDataAdapter
来填充DataTable
,然后使用Linq-To-DataTable来获取所需内容。另外,请始终使用using
- 声明:
Dim table = New DataTable()
Using conn As New SqlConnection("connection string here")
Using da = New SqlDataAdapter("select ........ ", conn)
da.Fill(table)
End Using
End Using
Dim matchingFamily = From row In table.AsEnumerable()
Where row.Field(Of String)("Family") = "family a"
If matchingFamily.Any() Then
Dim familyIssueCount As Int32 = matchingFamily.First().Field(Of Int32)("Issues")
' ... '
End If
如果多个行可能包含此族,则可能使用不同的方法,因为First
将选择任意行。一种方法是使用For Each
枚举它们或使用matchingFamily.Sum(Function(r) r.Field(Of Int32)("Issues"))
对问题求和。
如果您不需要内存中的所有记录,您也可以使用...WHERE Family = @family
仅从数据库中选择相关记录。
答案 1 :(得分:-1)
Private Sub GetFamilyIssues(ByVal FamilyName As String)
Dim cn As New SqlConnection("connection string here")
cn.Open()
Dim sql As String = "select * from FamilyIssues where FAMILY = @Family"
Dim cmd As New SqlCommand(sql, cn)
cmd.Parameters.AddWithValue("@Family", FamilyName)
Dim reader As SqlDataReader = cmd.ExecuteReader
txtIssues.Text = ""
If reader.HasRows Then
txtIssues.Text = reader("ISSUES").ToString()
End If
cn.Close()
End Sub