从数据库中检索值 [英] retrieve values from database

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

问题描述

我有一张我在vs 2008中创建的表格。我编写了一个代码来在数据库中插入值。它成功执行。但是如何编写一个代码来解释我输入的所有值或不使用seqel server 2008 r2等等。我的代码插入值如下:



< pre lang =c#> SqlConnection connection1 = new SqlConnection();
SqlCommand Insert = new SqlCommand();
Insert.Connection = connection1;
connection1.ConnectionString =( @ Data Source = .\SQLEXPRESS; AttachDbFilename = C:\ Users \ Acer \Documents\employeeDB.mdf; Integrated Security = True; Connect Timeout = 30; User Instance = True);
int a = Convert.ToInt32(txtEmpId.Text);
string b = txtEmpName.Text;
int c = Convert.ToInt32(txtSalary.Text);
string d = txtCity.Text;
Insert.CommandText = INSERT INTO Employeetable(EmpId,EmpName,Salary,City)值(@a, @ b,@ C,@ d);
Insert.Parameters.AddWithValue( @ a,a);
Insert.Parameters.AddWithValue( @ b,b);
Insert.Parameters.AddWithValue( @ c,c);
Insert.Parameters.AddWithValue( @ d,d);
Insert.Connection.Open();
Insert.ExecuteNonQuery();
Insert.Connection.Close();
Response.Write( 您已成功插入数据);
}。



谢谢。告诉我在VS上创建的读取按钮上写的代码。

解决方案

它非常相似,但您需要更改查询,并使用不同的功能。有两种基本方法:一种使用DataReader,另一种使用DataAdapter:

 使用(SqlConnection con =  new  SqlConnection(strConnect))
{
con.Open();
使用(SqlCommand com = new SqlCommand( SELECT iD,description FROM myTable WHERE Id = @ID,con))
{
com..Parameters.AddWithValue ( @ ID,myTextBox.Text);
使用(SqlDataReader reader = com.ExecuteReader())
{
while (reader.Read())
{
int id =( int )reader [ iD];
string desc =( string )reader [ description];
Console.WriteLine( ID:{0} \ n {1}, iD,desc);
}
}
}
}



 使用(SqlConnection con =  new  SqlConnection(strConnect))
{
con.Open();
使用(SqlDataAdapter da = new SqlDataAdapter( SELECT iD,description FROM myTable WHERE Id = @ID,con))
{
da.SelectCommand.Parameters。 AddWithValue( @ ID,myTextBox.Text);
DataTable dt = new DataTable();
da.Fill(dt);
myDataGridView.DataSource = dt;
}
}


I have a table that i have created in vs 2008 only. i write a code to insert values in database.And it executed succesfully. but how to write a code that dislay me all the entered values or without using seqel server 2008 r2 or so on.my code for inserting values is as follows:

SqlConnection connection1 = new SqlConnection();
      SqlCommand Insert = new SqlCommand();
      Insert.Connection = connection1;
      connection1.ConnectionString = (@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Acer\Documents\employeeDB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
      int a = Convert.ToInt32(txtEmpId.Text);
      string b = txtEmpName.Text;
      int c = Convert.ToInt32(txtSalary.Text);
      string d = txtCity.Text;
      Insert.CommandText = "INSERT INTO Employeetable(EmpId,EmpName,Salary,City) Values (@a,@b,@c,@d)";
      Insert.Parameters.AddWithValue("@a", a);
      Insert.Parameters.AddWithValue("@b", b);
      Insert.Parameters.AddWithValue("@c", c);
      Insert.Parameters.AddWithValue("@d", d);
        Insert.Connection.Open();
      Insert.ExecuteNonQuery();
      Insert.Connection.Close();
      Response.Write("you have succesfully inserted data");
  }.


Thanks.tell me what code to write on read button that i have created on VS.

解决方案

It's very similar, but you need to change the query, and use a different function. There are two basic ways: one uses a DataReader, the other a DataAdapter:

using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand com = new SqlCommand("SELECT iD, description FROM myTable WHERE Id = @ID", con))
        {
        com..Parameters.AddWithValue("@ID", myTextBox.Text);
        using (SqlDataReader reader = com.ExecuteReader())
            {
            while (reader.Read())
                {
                int id = (int) reader["iD"];
                string desc = (string) reader["description"];
                Console.WriteLine("ID: {0}\n    {1}", iD, desc);
                }
            }
        }
    }


using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlDataAdapter da = new SqlDataAdapter("SELECT iD, description FROM myTable WHERE Id = @ID", con))
        {
        da.SelectCommand.Parameters.AddWithValue("@ID", myTextBox.Text);
        DataTable dt = new DataTable();
        da.Fill(dt);
        myDataGridView.DataSource = dt;
        }
    }


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

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