- Home /
SQL connection Problem
Hi guys. I need some help please. I tried make an INSERT into my SQLDB but i cant because give me an error in this code line:
"commandoSQL.Connection = dbcon;"
the system send me this mensage:
"Assets/NGUI/Scripts/Interaction/ChamarVariavel.cs(43,29): error CS0266: Cannot implicitly convert type System.Data.IDbConnection' to
System.Data.SqlClient.SqlConnection'. An explicit conversion exists (are you missing a cast?)"
I hope somebody can help me with this. Thanks
public class ChamarVariavel : MonoBehaviour {
public UISlider slider;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnGUI(){
// Connection DB
string connectionString = "Data Source=(local);Initial Catalog=Test;User ID=******;Password=*******";
IDbConnection dbcon;
dbcon= new SqlConnection(connectionString);
dbcon.Open();
//DB Online
float x = slider.value * 100;
GUI.Label(new Rect( 570, 238, 70, 30 ), "(" + x.ToString("f2") + ")");
string qInsert = string.Format(@"INSERT INTO Fuel (fuel) VALUES ('{0}')", x);
SqlCommand commandoSQL = new SqlCommand(qInsert);
commandoSQL.Connection = dbcon;
try
{
commandoSQL.ExecuteNonQuery();
}
catch (Exception ex)
{
GUI.Label(new Rect( 300, 40, 300, 300 ), ex.ToString());
}
dbcon.Close();
//DB offline
}
}
Answer by Graham-Dunnett · Jun 13, 2014 at 04:31 PM
The error message says that SqlConnection()
returns a SqlConnection
but you are trying to assign to a IDbConnection
. So, either make a case to a IDbConnection
or change dbcon
to be a SqlConnection
.
Thank you, i follow your advice and it's worked. Problem solved. Thank you again.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
A Couple Questions on Networking 0 Answers
cannot connect unity to sql server 2012 0 Answers
Scenario connection Problem 0 Answers