我有一個帶有MySql數據庫的ASP.NET C#中製作的webform。數據庫包含一個名爲id的字段,它是主要的,自動遞增的和INT類型的。如何將MySql選擇轉換爲字符串?
我想選擇一個特定的記錄,比如數字10,將它轉換爲一個字符串,這樣我就可以將它與另一個字符串進行比較。
編輯,在全碼:
public partial class index : System.Web.UI.Page
{
// ConnString
String SqlConnString;
String qs;
private MySqlCommand SqlCommand;
private MySqlConnection dbConnection;
protected void btnSubmit_Click(object sender, ImageClickEventArgs e)
{
if (Page.IsValid)
{
// Variabele
string textbox = tbTextbox.Text;
// DbConnection
SqlConnString = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString.ToString();
SqlCommand = new MySqlCommand();
dbConnection = new MySqlConnection();
dbConnection.ConnectionString = SqlConnString;
// qs
qs = "INSERT INTO applys" +
"(textbox, date)" +
" VALUES (@textbox, NOW())";
// Parameters
SqlCommand.Parameters.Clear();
SqlCommand.Parameters.AddWithValue("@textbox", date);
//open database
dbConnection.Open();
SqlCommand.CommandText = qs;
SqlCommand.Connection = dbConnection;
SqlCommand.ExecuteNonQuery();
SqlCommand.Dispose();
dbConnection.Dispose();
///////////
MySqlDataReader reader = null;
MySqlConnection con = null;
MySqlCommand cmd = null;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
string ConnectionString = "connection string";
con = new MySqlConnection(ConnectionString);
string query = "SELECT id FROM apply WHERE id = 10";
cmd = new MySqlCommand(query);
cmd.Connection = con;
try
{
// Open Connection
con.Open();
// Execute the query
reader = cmd.ExecuteReader();
bool found = false;
while (reader.Read())
{
if (reader["id"].ToString() == "10")
{
found = true;
break;
}
}
if (found)
{
ltl.Text = "Worked!";
//Response.Redirect("different page");
}
else
ltl.Text = "Nope";
//Response.Write("Nothing!");
}
catch
{
// MessageBox.Show("Error!");
}
int id = -1;
id = (Int32)cmd.ExecuteScalar();
if (id != -1)
ltl.Text = "Worked!";
//Response.Redirect("different page");
reader.Dispose();
reader.Close();
con.Dispose();
con.Close();
cmd.Connection.Close();
cmd.Dispose();
}
}
}
你是否真正執行該查詢,或者只是一些餡SQL文本到一個變量? – 2012-07-31 22:01:18
是的,查詢正在執行。該表單的目標是,如果用戶在id字段中爲例如數字10,則我使用if else將它們重定向到不同的頁面。 我意識到,比較字符串不是最好的方法... – Yuri 2012-07-31 22:12:05
如果您發佈涉及此代碼,它會更容易幫助。 – joar 2012-08-01 02:29:25