저는 ASP.NET C#에서 만든 Webform을 MySql 데이터베이스와 함께 가지고 있습니다. 데이터베이스에는 id, auto increment 및 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 텍스트를 채우고 있습니까? –
예, 쿼리가 실행 중입니다. 양식의 목표는 사용자가 예를 들어 id 필드에서 숫자 10 인 경우, if else를 사용하여 다른 페이지로 리디렉션하는 것입니다. 문자열을 비교하는 것이 최상의 방법이 아니라는 것을 알고 있습니다 ... – Yuri
이 코드를 게시하면 도움이 될 것입니다. – joar