简体   繁体   English

在asp.net中连接的SQL Server数据库文件但查询未执行

[英]SQL Server database file connected in asp.net but query not executing

I connected a .MDF database file to my aspx file in C# (Visual Studio), but when I'm trying to execute a query it doesn't work. 我将.MDF数据库文件连接到C#(Visual Studio)中的aspx文件,但是当我尝试执行查询时,它不起作用。

Given below is the code where the problem occurs. 下面给出的是发生问题的代码。 Please help me out. 请帮帮我。

The com.ExecuteNonQuery() returns -1, I guess it should return 1 in case of a successful execution com.ExecuteNonQuery()返回-1,我猜如果成功执行,它应该返回1

SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\karunya\Documents\Visual Studio 2010\Projects\Login\Login\App_Data\MyDB.mdf;Integrated Security=True;User Instance=True");

con.Open();
string sql = "SELECT * FROM Data WHERE user = '"+user.Text+"' ";
SqlCommand com = new SqlCommand(sql, con);
int tmp = Convert.ToInt32(com.ExecuteNonQuery());

con.Close();

if (tmp == 1)
{
    con.Open();
    sql = "SELECT pass FROM LogTable WHERE user = '" + user.Text + "'";
    SqlCommand com1 = new SqlCommand(sql, con);
    string password = com1.ExecuteScalar().ToString();

    if (password == pass.Text)
    {
       Response.Write("Access Granted!");
    }
    else
    {
       Response.Write("Access Denied!");
    }
 }
 else
 {
    Response.Write("User Name Incorrect!");
 }

what you trying to do ? 您想做什么?

it will always return -1 because you are selecting tables 它总是返回-1,因为您正在选择表

do this .. 做这个 ..

 DataTable dt = new DataTable();
        using (var conn = new SqlConnection(connectionLib))
        {
            try
            {
                using (var cmd = new SqlCommand(Query, conn))
                {

                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    da.Fill(dt);
                    conn.Close();
                    da.Dispose();
                }
            }
            catch
            {
            }
        }

now check condition according to you if you want to check no of row then check 现在根据您的条件检查是否要检查行数,然后检查

dt.rows.cout

Instead of relying on the return value of ExecuteNonQuery , use a COUNT(*) inside the query and return the count using ExecuteScalar . 不用依赖ExecuteNonQuery的返回值,而是在查询内部使用COUNT(*)并使用ExecuteScalar返回计数。

And always (ALWAYS) use parameterized queries: 并且总是(总是)使用参数化查询:

string sql = "SELECT COUNT(*) FROM Data WHERE user = @user";
SqlCommand com = new SqlCommand(sql, con);
com.Parameters.AddWithValue("@user", user.Text);
int tmp = Convert.ToInt32(com.ExecuteScalar());

And move the call to Close() towards the end of the method 并将对Close()的调用移到方法的结尾

please try to check if the connections string it's ok then try this code 请尝试检查连接字符串是否正常,然后尝试以下代码

you will know if the select hjas rows or not than do what you want to do 您会知道是否选择了hjas行,而不是想要执行的操作

       con.Open();
                            cmd.Connection = con;
                            cmd.CommandText = "select * from user where login=@login and pass=@psw";
                            cmd.Parameters.Add(new SqlParameter("@login", SqlDbType.VarChar, 255)).Value = txtpsedu.Text;
                            cmd.Parameters.Add(new SqlParameter("@psw", SqlDbType.VarChar, 255)).Value = txtlogin.Text;

   dr = cmd.ExecuteReader();

                    if (dr.Read())
                    {

                   //// do  what  you  want  here  

                    }

                    else
                    {

                        Response.Write("User Name Incorrect!");

                    }
                    dr.Close();
                    con.Close();

i hope to try it 我希望尝试一下

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM