开发者

Prepared SELECT statement in .Net

开发者 https://www.devze.com 2023-03-09 08:34 出处:网络
I can\'t understand what I am doing wrong, I can\'t seem to SELECT with a pre开发者_运维技巧pared statement. However I can INSERT with a prepared statement.

I can't understand what I am doing wrong, I can't seem to SELECT with a pre开发者_运维技巧pared statement. However I can INSERT with a prepared statement.

MySqlCommand cmd = new MySqlCommand("SELECT * FROM code_post WHERE name = ?postRequired LIMIT 1",  dbcon);
cmd.Parameters.Add(new MySqlParameter("?postRequired", requestString));
cmd.ExecuteNonQuery();

DataSet ds = new DataSet();
cmd.fill(ds, "result");
try {
             thisBlog = ds.Tables["result"].Rows[0];
} catch {
             invalid();
             return;
}

Any advice on this would be greatly appreciated!


To fill a DataSet you will need a DataAdapter.

Try this:

MySqlCommand cmd = new MySqlCommand("SELECT * FROM code_post WHERE name = ?postRequired LIMIT 1",  dbcon);
cmd.Parameters.Add(new MySqlParameter("?postRequired", requestString));
cmd.ExecuteNonQuery();

DataSet ds = new DataSet();
MySqlDataAdapter dAdap = new MySqlDataAdapter();
dAdap.SelectCommand = cmd;
dAdap.Fill(ds, "result");
try {
             thisBlog = ds.Tables["result"].Rows[0];
} catch {
             invalid();
             return;
}


You need to use SqlDataAdapter

DataAdapter represents a set of data commands and a database connection that are used to fill the DataSet and update a SQL Server database.

The SqlDataAdapter provides this bridge by mapping Fill, which changes the data in the DataSet to match the data in the data source

Check the following syntax:

private static DataSet SelectRows(DataSet dataset,
    string connectionString,string queryString) 
{
    using (SqlConnection connection = 
        new SqlConnection(connectionString))
    {
        SqlDataAdapter adapter = new SqlDataAdapter();
        adapter.SelectCommand = new SqlCommand(
            queryString, connection);
        adapter.Fill(dataset);
        return dataset;
    }
}
0

精彩评论

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