1

私は次の問題に直面しています: Access 2010 にフィールドNICActiveおよびを含むデータベースがありPage、すべてが数値型です。NICユーザーからの入力として (数値) を受け取り、NIC に従って特定のページにリダイレクトするログイン ページを作成したいと考えています。

人によって異なるページが表示されます。ExecuteScalarコマンドでエラーが発生しました。クエリが正しくないかExecuteScalar、クエリを保持できない可能性がありdata type mismatchます。エラーが発生しています。

try
{
    FirsstPage f = new FirsstPage();
    SecondPage second = new SecondPage();
    oledcon.Open();

    string NIc = ( TextBox1.Text);
    // string query = "select * from LogINTable where NIC='" + NIc + "'AND Active=0 AND page=1";
    //string query = "select * from LogINTable where NIC='" + nic + "'AND Active=0";
    string query = "SELECT * FROM LogINTable WHERE NIC= '" + NIc + "' AND Active=0 AND page=1";
    //string query = "select
    OleDbCommand comm = new OleDbCommand( query,oledcon);
    string a = (string) comm.ExecuteScalar();
    if (a != null)
    {
        Response.Redirect("FirsstPage.aspx");
        string update = "update into LogINTable Active='1' where NIC='" + NIc + "' ";
        //OleDbCommand com = new OleDbCommand();
        //int b = Convert.ToInt32( com.ExecuteScalar());
    }
    else
    {
        Response.Redirect("SecondPage.aspx");
        string update = "update into LogINTable Active='1' where NIC='" +NIc + "' ";
    }

    oledcon.Close();
}
catch (Exception ex)
{
    Label1.Text = ex.Message;
}
finally 
{
    oledcon.Close();
}
4

1 に答える 1

1

問題は、間違ったクエリで ExecuteScalar を使用していることです。

string a = (string) comm.ExecuteScalar();

ExecuteScalar() は、クエリの結果として単一の値を返します。

クエリを、列全体の代わりにデータベースから単一の値を返すブローのようなクエリに変更してください

Select NIC FROM LogINTable WHERE NIC= '" + NIc + "' AND Active=0 AND page=1" 

ソース: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar.aspx

お役に立てば幸いです。

于 2013-05-13T09:25:20.620 に答える