Friday, February 24, 2012

Convert MS acess to SQL Server

if (!Page.IsPostBack)

{ if (Session["users"] != null && (Session[flag"] == "true"))

{

String IP = Request.ServerVariables["remote_host"].ToString();

String Datee = DateTime.Now.Date.ToString();

OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data source="

+ HttpContext.Current.Server.MapPath("~/App_Data/result.mdb"));

OleDbCommand cmd = new OleDbCommand("Insert Into KullaniciSayisi (IP,Datee) Values (IPDatee)", con);

cmd.Parameters.AddWithValue("IP", IP);

cmd.Parameters.AddWithValue("Datee", Datee);

con.Open();

intresultt = cmd.ExecuteNonQuery();

con.Close();

Session["flag"] = "false";

}

}

I want to convert this code to SQL sever . However I can not do it? Can you help me?

Hi,

1) Change OleDbConnection to SqlClient.SqlConnection

2) Change OleDbCommand to SqlClient.SqlCommand

3) Change the connection string to connect into your local SQL server, you can get more info from www.connectionstrings.com

That is all what you need.

|||The name "IP" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted.|||

I convert however I have a problem. my code do not work :(

String IP=...;

String Datee=DateTime.Now.ToShortDateString();

............

SqlParameter paramIP=new SqlParameter("@.IP",SqlDbType.varchar,50);

SqlParameter paramdate=new SqlParameter("@.IP",SqlDbType.datetime);

paramIP.value=IP;

paramdate.value=Convert.ToDateTime(Datee).ToShortDateString();

cmd.Parameters.AddWithValue("@.IP",paramIP);

cmd.Parameters.AddWithValue("@.date",paramdate);

con.Open();

cmd.ExecuteNonQuery();

con.Close();

When I debug the code cmd.ExecuteNon.Query() give problem. In my db date type is Datetime, IP is String. Can you help me?

|||Post the whole code and the exception as well.|||

if (!Page.IsPostBack)

{

if (Session["ziyaretci"] != null && (Session["kontrol"] == "true"))

{

try{

String IP = Request.ServerVariables["remote_host"].ToString();

String Tarih = DateTime.Now.Date.ToString();

SqlConnection= my connection string

SqlCommand cmd = new OleDbCommand("Insert Into userss (IP,Datee) Values (IP,Datee)", con);

SqlParameter paramIP=new SqlParameter("@.IP",SqlDbType.varchar,50);

SqlParameter paramdate=new SqlParameter("@.IP",SqlDbType.datetime);

paramIP.value=IP;

paramdate.value=Convert.ToDateTime(Datee);

cmd.Open();

int result = cmd.ExecuteNonQuery();

cmd.Close();

Session["kontrol"] = "false";

}

catch(Exception ex){

Response.Write("There is a problem" + ex)

}

}

}

This is my all code.

|||

Hi,

Change the followings

SqlCommand cmd = new OleDbCommand("Insert Into userss (IP,Datee) Values (@.IP,@.Date)", con);

SqlParameter paramIP=new SqlParameter("@.IP",SqlDbType.varchar,50);

SqlParameter paramdate=new SqlParameter("@.Date",SqlDbType.datetime);


|||

Thanks for your helping.

No comments:

Post a Comment