开发者

After creating a database with Visual Studio, on SQL Server 2008, how do I create a form to query the data?

开发者 https://www.devze.com 2023-04-11 14:01 出处:网络
I want to create a database in Visual S开发者_如何学Gotudio. I will be using SQL Server 2008. After I create it, I want to lets say.. I have created a code

I want to create a database in Visual S开发者_如何学Gotudio. I will be using SQL Server 2008.

After I create it, I want to lets say.. I have created a code

EG:

select *
From table x
where bus = @value
Group by timetable

How can I have a form field that I can enter the @value into and then it will give me the results?


This is a very broad question. You have a data source (SQL Server database) and now you have to create the connection parameters. This is called a Connection String. The connection string is what an application uses to connect to an instance\database within a specific security context with specified credentials.

Then inside the application you can utilize System.Data.SqlClient namespace. Here you will use the SqlConnection class to connect to the data source and the SqlCommand class to issue a command against the connection. At the bottom of that page is a decent code snippet:

private static void ReadOrderData(string connectionString)
{
    string queryString = 
        "SELECT OrderID, CustomerID FROM dbo.Orders;";
    using (SqlConnection connection = new SqlConnection(
               connectionString))
    {
        SqlCommand command = new SqlCommand(
            queryString, connection);
        connection.Open();
        SqlDataReader reader = command.ExecuteReader();
        try
        {
            while (reader.Read())
            {
                Console.WriteLine(String.Format("{0}, {1}",
                    reader[0], reader[1]));
            }
        }
        finally
        {
            // Always call Close when done reading.
            reader.Close();
        }
    }
}

It utilizes a DataReader as opposed to a DataAdapter to retrieve data. If you are just looking to do a SELECT against the database a SqlCommand.ExecuteNonQuery() call will be sufficient.

That's a start, but it's hardly the tip of the iceberg with ADO.NET and the surrounding technologies that you need to know and implement for this type of programming.

0

精彩评论

暂无评论...
验证码 换一张
取 消