Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
659 views
in Technique[技术] by (71.8m points)

c# - Format of the initialization string does not conform to to specification starting at index 0

I am using Microsoft Enterprise Lip I I have this method to Insert resource in the website I get this error down i don't think it is permission problem and really i don't know how to solve it.by the way I test the connectionStrings and it work fine

  <connectionStrings>
    <add name="SiteSqlServer"
      connectionString="Data Source=.;Initial Catalog=databaseName;User ID=sa;Password=***"/>
  </connectionStrings>

 public static int Insert(Resoursce r)
        {
            Database objDB = new SqlDatabase("SiteSqlServer");
            int val = 0;
            using (DbCommand cmd = objDB.GetStoredProcCommand("InsertResoursce"))
            {
                //  OutParameter
                objDB.AddOutParameter(cmd,"@OutResoursceID",DbType.Int32,int.MaxValue);

                //  iNParameter
                objDB.AddInParameter(cmd, "@ModuleId", DbType.Int32, r.ModuleId);
                objDB.AddInParameter(cmd, "@Summary", DbType.StringFixedLength, r.Summary);
                objDB.AddInParameter(cmd, "@PageId", DbType.StringFixedLength, r.PageID);
                objDB.AddInParameter(cmd, "@TypeId", DbType.Int32, r.TypeID);
                objDB.AddInParameter(cmd, "@UserID", DbType.Guid, r.UserID);
                objDB.AddInParameter(cmd, "@Enabled", DbType.Boolean, r.Enabled);
                objDB.AddInParameter(cmd, "@SafetyAlert", DbType.Boolean, r.SafetyAlert);
                objDB.AddInParameter(cmd, "@SaftyAlertText", DbType.StringFixedLength, r.SafetyAlertText);


                try
                {
                     val = objDB.ExecuteNonQuery(cmd);
                     if (val == 1)
                     {
                         return Convert.ToInt32(objDB.GetParameterValue(cmd, "@OutResoursceID"));
                     }
                     else
                     {
                         return -1;
                     }
                }
                catch (Exception ex)
                {


                    throw ex;
                } 

            }

System.ArgumentException was caught HResult=-2147024809
Message=Format of the initialization string does not conform to specification starting at index 0. Source=System.Data StackTrace: at System.Data.Common.DbConnectionOptions.GetKeyValuePair(String connectionString, Int32 currentPosition, StringBuilder buffer, Boolean useOdbcRules, String& keyname, String& keyvalue) at System.Data.Common.DbConnectionOptions.ParseInternal(Hashtable parsetable, String connectionString, Boolean buildChain, Hashtable synonyms, Boolean firstKey) at System.Data.Common.DbConnectionOptions..ctor(String connectionString, Hashtable synonyms, Boolean useOdbcRules) at System.Data.SqlClient.SqlConnectionString..ctor(String connectionString) at System.Data.SqlClient.SqlConnectionFactory.CreateConnectionOptions(String connectionString, DbConnectionOptions previous) at System.Data.ProviderBase.DbConnectionFactory.GetConnectionPoolGroup(DbConnectionPoolKey key, DbConnectionPoolGroupOptions poolOptions, DbConnectionOptions& userConnectionOptions) at System.Data.SqlClient.SqlConnection.ConnectionString_Set(DbConnectionPoolKey key) at System.Data.SqlClient.SqlConnection.set_ConnectionString(String value) at Microsoft.Practices.EnterpriseLibrary.Data.Database.CreateConnection() at Microsoft.Practices.EnterpriseLibrary.Data.Database.GetNewOpenConnection() at Microsoft.Practices.EnterpriseLibrary.Data.Database.GetWrappedConnection() at Microsoft.Practices.EnterpriseLibrary.Data.Database.GetOpenConnection() at Microsoft.Practices.EnterpriseLibrary.Data.Database.ExecuteNonQuery(DbCommand command) at Christoc.Modules.ResourceModule.App_Code.BOL.Resoursce.Insert(Resoursce r) in c:inetpubwwwrootideaParkDesktopModulesResourceModuleApp_CodeBOLResoursce.cs:line 54 InnerException:

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This usually means your connection string isn't any good. If you look at the stack trace, you'll notice that this is failing when trying to interpret your connection string.

Check your connection string to make sure it is correct - or post it here for help (but without any sensitive information such as passwords ;) )

UPDATE

According to the SqlDatabase documentation the SqlDatabase class takes a connection string, not a key to the connection string configuration.

So

new SqlDatabase("SiteSqlServer");

Should be

var connection = ConfigurationManager.ConnectionStrings["SiteSqlServer"];

Database objDB = new SqlDatabase(connection.ConnectionString);

(I have omitted any defensive code here for brevity)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...