2017-06-23 18 views
0

WindowsにXAMPPがインストールされており、MySQLがセットアップされています。C#でMariaDBデータベースを照会

私はC#から自分のデータベースにどのように問い合わせることができるのだろうかと思っていました。

すでにMySql.Data.MySqlClient.MySqlConnectionを使用して接続できます。

データベース内の文字列を探していて、それがある場合はFound!というmessageboxをポップアップします。どうすればいい?ここで

+0

ボタンクリックイベントにDB

private string GetValueFromDBUsing(string strQuery) { string strData = ""; try { if (string.IsNullOrEmpty(strQuery) == true) return string.Empty; using (var mysqlconnection = new MySqlConnection(m_strMySQLConnectionString)) { mysqlconnection.Open(); using (MySqlCommand cmd = mysqlconnection.CreateCommand()) { cmd.CommandType = CommandType.Text; cmd.CommandTimeout = 300; cmd.CommandText = strQuery; object objValue = cmd.ExecuteScalar(); if (objValue == null) { cmd.Dispose(); return string.Empty; } else { strData = (string)cmd.ExecuteScalar(); cmd.Dispose(); } mysqlconnection.Close(); if (strData == null) return string.Empty; else return strData; } } } catch (MySqlException ex) { LogException(ex); return string.Empty; } catch (Exception ex) { LogException(ex); return string.Empty; } finally { } } 

から、あなたの関数のコードを文字列値を得るためにあなたのデータベース

string m_strMySQLConnectionString; m_strMySQLConnectionString = "server=localhost;userid=root;database=dbname"; 

機能に接続させるためのサンプルコードですあなたは何を試しましたか? – tym32167

答えて

0

は、アプリケーションが

try 
    { 
    string strQueryGetValue = "select columnname from tablename where id = '1'"; 
    string strValue = GetValueFromDBUsing(strQueryGetValue); 
    if(strValue.length > 0) 
    { 
      MessageBox.Show("Found"); 
      MessageBox.Show(strValue); 
    } 

    else 
     MessageBox.Show("Not Found");   
    } 
    catch(Exception ex) 
    { 
     MessageBox.Show(ex.Message.ToString()); 
    } 
関連する問題