1

connection object & some ado.net part getting some error while insert to the data,first of all here is my PHP coding part.Here i'm going to convert php codings to .net

openConn();
//<user> slett <slettID>
if((sizeof($MessageArray)==3) && (strtolower($MessageArray[1])=='slett'))
 {

$user_id=$MessageArray[2];
if ((int)$user_id > 0)
{
    $username=$MessageArray[0];
    $QueryCheckMak = "SELECT COUNT(*) FROM XYZ WHERE id='$user_id'";
    $QueryCheckMak = odbc_exec($conn, $QueryCheckMak) or die("Query error");
    $user_number = odbc_result($QueryCheckMak, 1);

    if($user_number=="0")
    {
        $message_out = "SOME ERROR OUT ";
    }

Below coding is my try to convert it.I successfully convert.sizeof part & string to lower case part.below that here coding part is used ODBC connection but in my case i sue sql server connection

public partial class SeenSMS : System.Web.UI.UserControl
{

    public SqlConnection mycon;
    SqlConnection con = new SqlConnection(@"Data Source=DSOFT\DOMZ;Initial                 Catalog=Vilt;UserID=sa;Password=sa123");


    protected void Page_Load(object sender, EventArgs e)
    {
        string[] msg_arr = Request.QueryString["arr"].Split('|');

        if (!IsPostBack)
        {
            string Moose = Request.QueryString[1];
        }

        if (msg_arr != null)
        {
            if ((msg_arr.Length == 3) && (msg_arr[1].ToLower() == "slett"))

            {
                int user_id = Convert.ToInt32(msg_arr[2]); 

                if(user_id > 0)
                {
                    string username = msg_arr[0]; 

                    SqlDataReader rdr = null;
                    con.open();
                    SqlCommand cmd = new SqlCommand("SELECT COUNT(*) FROM XYZ WHERE id='$user_id",con);

                    if(user_number =='0')
                       {
                     Response.WriteLine("ERROR");

                     // else part here
                    }

                }
            }


        }
    }
4

2 回答 2

4

Use this type of syntax, the using block will automatically dispose of the objects at the end of the scope

public partial class SeenSMS : System.Web.UI.UserControl
{
    string connectionString = "Data Source=DSOFT\DOMZ;Initial Catalog=Vilt;UserID=sa;Password=sa123";
    protected void Page_Load(object sender, EventArgs e)
    {
        ....       
        using (SqlConnection con = new SqlConnection(connectionString))
        {
            con.Open();
            using (SqlCommand command = new SqlCommand("SELECT COUNT(*) FROM TableName WHERE id='" +user_id +"', con))
            using (SqlDataReader reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    response.Write(reader.GetInt32(0), reader.GetString(1), reader.GetString(2));
                }
            }
        }
        ....
    }
}
于 2012-06-10T18:05:39.590 回答
2

try this:

 SqlCommand cmd = new SqlCommand("SELECT COUNT(*) FROM XYZ WHERE user_id=@user_id,con));
 oCmd.Parameters.AddWithValue("@user_id", user_id);

 int user_number = 0;                 
 con.open();
   using (SqlDataReader oReader = cmd.ExecuteReader())
            {                   
                while (oReader.Read())
                {
                   user_number =Convert.ToInt32(oReader["count"]);                                                           
                }
                myConnection.Close();
            }                   



           if(user_number == 0)
                   {
                 Response.WriteLine("ERROR");

                 // else part here
                }
于 2012-06-10T17:57:39.677 回答