I have a website which is set to query a database for a username, and then populate the Session[]
variables with the user's info. When I query using the server's stored procedure, I get back the entire row. How can I split apart the row and parse it so that every column gets parsed separately?
My code for the button is:
protected void Button4_Click(object sender, EventArgs e)
{
string strConn = WebConfigurationManager.ConnectionStrings["TicketsConnectionString"].ConnectionString;
SqlConnection myConnection = new SqlConnection(strConn);
//Search button
string sqlText = ("select * from [dbo].[User]");
SqlCommand myCommand = new SqlCommand(sqlText,myConnection);
myCommand.CommandType = CommandType.Text;
myConnection.Open();
SqlDataReader objReader;
objReader = myCommand.ExecuteReader();
objGrid.DataSource = objReader;
objGrid.DataBind()开发者_如何转开发;
}
UPDATE
I updated it to string sqlText = ("select 'UserID', 'FirstName' from [dbo].[User] where UserID='"+txtTest.Text+"' and Password='" + txtPass.Text+"'");
But now I get the following column Column1 Column2 UserID FirstName
To read a column's value use this:
avar = row["colname"];
Instead of:
string sqlText = ("select * from [dbo].[User]");
Use:
string sqlText = ("select username from [dbo].[User]");
That will return just the column you need.
* UPDATE *
Making a series of queries for all the properties is definitely not a good option. You can select all the columns you need by separating by comma like this:
string sqlText = ("select username, otherfield, evenAnotherField from [dbo].[User]");
Then you can use the SqlDataReader indexer to get the values you need:
var username = objReader["username"];
精彩评论