- Home /
problem in database connection
hi guys,
I'm trying to connect Sqlite database into "server : localhost" and i'm getting some strange errors..can any one explain what mistake i have done??
the error is: Code:
1.System.ArgumentException: Invalid ConnectionString format for parameter "Server : localhost" at Mono.Data.Sqlite.SqliteConnection.ParseConnectionString () [0x00000] in :0 at Mono.Data.Sqlite.SqliteConnection.Open () [0x00000] in :0 at (wrapper remoting-invoke-with-check) Mono.Data.Sqlite.SqliteConnection:Open () at commonDatabaseScript.openConnection () [0x00011] in E:\sriram\projects\databa\Assets \commonDatabaseScript.cs:40 UnityEngine.MonoBehaviour:print(Object) commonDatabaseScript:openConnection() (at Assets/commonDatabaseScript.cs:45) commonDatabaseScript:insertData(String) (at Assets/commonDatabaseScript.cs:52) commonDatabaseScript:OnGUI() (at Assets/commonDatabaseScript.cs:109)
2.InvalidOperationException: Database is not open Mono.Data.Sqlite.SqliteCommand.InitializeForReader () Mono.Data.Sqlite.SqliteCommand.ExecuteReader (CommandBehavior behavior) Mono.Data.Sqlite.SqliteCommand.ExecuteReader () (wrapper remoting-invoke-with-check) Mono.Data.Sqlite.SqliteCommand:ExecuteReader () commonDatabaseScript.doQuery () (at Assets/commonDatabaseScript.cs:70) commonDatabaseScript.OnGUI () (at Assets/commonDatabaseScript.cs:110)
can any one say wat's going on pls???
and here is my code: public static void openConnection() { string connectionString = "Server : localhost ; Database =testdata.db "; try { dbConnection = new SqliteConnection (connectionString); dbConnection.Open(); } catch(Exception e) { string temp1 = e.ToString(); print(temp1); } Debug.Log("Database connected"); }
public static void insertData(string x) { openConnection(); SqliteCommand dbCommand = dbConnection.CreateCommand(); dbCommand.CommandText = "INSERT into nametable(name1) values ('"+x+"');"; dbCommand.Dispose(); dbCommand = null; dbConnection.Close(); dbConnection = null; }
Answer by StephanK · Nov 12, 2010 at 09:13 AM
Your connection string is using the wrong syntax. This is an example how it should look like:
string Server = "localhost"; string Username = "my_username"; string Password = "my_password"; string Database = "my_database";
string ConnectionString = "Data Source=" + Server + ";"; ConnectionString += "User ID=" + Username + ";"; ConnectionString += "Password=" + Password + ";"; ConnectionString += "Initial Catalog=" + Database;
have i want to initialize all those into start() function?
string ConnectionString = "Data Source=" + Server +";" "UserID=" + Username +";" "Password=" + Password +";" "Initial Catalog=" + Database ";";
is it correct???
its Showing error as error CS1525: Unexpected symbol `UserID='
hi spree thanks for your reply,
but still i'm getting the same error which i have mentioned above,now my code is'
public static void openConnection() { string connectionString = "Server=localhost;" + "Database=testdata;" + "User ID=name;" + "Password=password";
try
{
dbConnection = new SqliteConnection (connectionString);
{
dbConnection.Open();
dbCommand.ExecuteNonQuery();
}
}
catch(Exception e)
{
string temp1 = e.ToString();
print(temp1);
}
Debug.Log("Database connected");
}
which error? and reading code in the comments section is really hard. maybe just edit your original question.
Answer by sriram90 · Nov 13, 2010 at 05:11 AM
ya i got the clear ans now.....these errors are came from "connectionString " syntax.The exact code for when you pass it from a function is,
public void OpenDB(string localhost, string db, string name,string password,string false)
{
connectionString = "host= " +localhost + ";Database= " +db + ";User ID= " +name + ";Password= " +password +";Pooling= " +false+ ";";
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613082827im_/https://answers.unity.com/themes/thub/images/avi.jpg)