从索引 0 处开始,初始化字符串的格式不符合规范。

时间:2022-03-12 13:05:11
我的APP.config

<configuration>
  <configSections>
  </configSections>
  
  <!--反射 -->
  <appSettings>
    <add key="DAL" value="BoteAutoWMS.SQLServerDAL"/>
  </appSettings>
  
  <!--连接字符串-->
  <connectionStrings>
    <add name="DBconnstring" connectionString="Server=.;uid=sa;pwd=123;database=BoteAutoCMS" providerName="System.Data.SqlClient" />
  </connectionStrings>

</configuration>

运行到

        /// <summary>
        /// 执行一条计算查询结果语句,返回查询结果(object)。
        /// </summary>
        /// <param name="SQLString">计算查询结果语句</param>
        /// <returns>查询结果(object)</returns>
        public static object GetSingle(string SQLString, params SqlParameter[] cmdParms)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))----------运行到这出现从索引 0 处开始,初始化字符串的格式不符合规范。
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    try
                    {
                        PrepareCommand(cmd, connection, null, SQLString, cmdParms);
                        object obj = cmd.ExecuteScalar();
                        cmd.Parameters.Clear();
                        if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
                        {
                            return null;
                        }
                        else
                        {
                            return obj;
                        }
                    }
                    catch (System.Data.SqlClient.SqlException e)
                    {
                        throw new Exception(e.Message);
                    }
                }
            }
        }

异常信息如下:
未处理 System.ArgumentException
  Message="从索引 0 处开始,初始化字符串的格式不符合规范。"
  Source="System.Data"
  StackTrace:
       在 System.Data.Common.DbConnectionOptions.GetKeyValuePair(String connectionString, Int32 currentPosition, StringBuilder buffer, Boolean useOdbcRules, String& keyname, String& keyvalue)
       在 System.Data.Common.DbConnectionOptions.ParseInternal(Hashtable parsetable, String connectionString, Boolean buildChain, Hashtable synonyms, Boolean firstKey)
       在 System.Data.Common.DbConnectionOptions..ctor(String connectionString, Hashtable synonyms, Boolean useOdbcRules)
       在 System.Data.SqlClient.SqlConnectionString..ctor(String connectionString)
       在 System.Data.SqlClient.SqlConnectionFactory.CreateConnectionOptions(String connectionString, DbConnectionOptions previous)
       在 System.Data.ProviderBase.DbConnectionFactory.GetConnectionPoolGroup(String connectionString, DbConnectionPoolGroupOptions poolOptions, DbConnectionOptions& userConnectionOptions)
       在 System.Data.SqlClient.SqlConnection.ConnectionString_Set(String value)
       在 System.Data.SqlClient.SqlConnection.set_ConnectionString(String value)
       在 System.Data.SqlClient.SqlConnection..ctor(String connectionString)
       在 BoteAutoWMS.DBUtility.SqlHelper.GetSingle(String SQLString, SqlParameter[] cmdParms) 位置 D:\coding\BoteAutoWMS\BoteAutoWMS\BoteAutoWMS.DBUtility\SqlHelper.cs:行号 472
       在 BoteAutoWMS.DBUtility.SqlHelper.Exists(String strSql, SqlParameter[] cmdParms) 位置 D:\coding\BoteAutoWMS\BoteAutoWMS\BoteAutoWMS.DBUtility\SqlHelper.cs:行号 57
       在 BoteAutoWMS.SQLServerDAL.AdminUser.CheckUserLogin(AdminInfo admininfo) 位置 D:\coding\BoteAutoWMS\BoteAutoWMS\SQLServerDAL\Users.cs:行号 32
       在 BoteAutoWMS.BLL.AdminUser.CheckUserLogin(AdminInfo admininfo) 位置 D:\coding\BoteAutoWMS\BoteAutoWMS\BoteAutoWMS.BLL\AdminUser.cs:行号 27
       在 BoteAutoWMS.frmLogin.lblSumbit_Click(Object sender, EventArgs e) 位置 D:\coding\BoteAutoWMS\BoteAutoWMS\BoteAutoWMS\frmLogin.cs:行号 97
       在 System.Windows.Forms.Control.OnClick(EventArgs e)
       在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       在 System.Windows.Forms.Control.WndProc(Message& m)
       在 System.Windows.Forms.Label.WndProc(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       在 System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
       在 System.Windows.Forms.Form.ShowDialog()
       在 BoteAutoWMS.Program.Main() 位置 D:\coding\BoteAutoWMS\BoteAutoWMS\BoteAutoWMS\Program.cs:行号 21
       在 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       在 System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

也查了不知道错到哪了,有经验的给指导下

9 个解决方案

#1


你看看出错时,connectionString变量的值是什么?

#2


你是如何得到 connectionString的

#3


给connectionString赋值了吗?
connectionString = System.ConfigurationManagment.ConnectionString["DBconnstring"];

#4


上面那个写错了,应该是这样的:
connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DBconnstring"].ConnectionString;

#5



connectionString="Server=.;uid=sa;pwd=123;database=BoteAutoCMS"

这里有问题吧 你用的petshop里的数据库访问部件吧 
用下面的格式试试看
Password=123;Persist Security Info=True;User ID=sa;Initial Catalog=test;Data Source=(loacl)

#6


你的连接字符串有问题,没有uid和pwd的写法,应该这样:
Data Source=.;Initial Catalog=BoteAutoCMS;User Id=sa;Password=123;

#7


Driver={SQL Native Client};Server=myServerAddress;Database=myDataBase; Uid=myUsername;Pwd=myPassword;

这样的连接字符串,可以用于ODBC连接方式,用 SqlConnection就不行了。

#8


你可以用vs自带的数据库连接控件来测试你的连接字符串;
测好后直接拷贝就行了

#9


引用 1 楼 computerfox 的回复:
你看看出错时,connectionString变量的值是什么?

connectionString=BoteAutoWMS.SQLServerDAL

#1


你看看出错时,connectionString变量的值是什么?

#2


你是如何得到 connectionString的

#3


给connectionString赋值了吗?
connectionString = System.ConfigurationManagment.ConnectionString["DBconnstring"];

#4


上面那个写错了,应该是这样的:
connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DBconnstring"].ConnectionString;

#5



connectionString="Server=.;uid=sa;pwd=123;database=BoteAutoCMS"

这里有问题吧 你用的petshop里的数据库访问部件吧 
用下面的格式试试看
Password=123;Persist Security Info=True;User ID=sa;Initial Catalog=test;Data Source=(loacl)

#6


你的连接字符串有问题,没有uid和pwd的写法,应该这样:
Data Source=.;Initial Catalog=BoteAutoCMS;User Id=sa;Password=123;

#7


Driver={SQL Native Client};Server=myServerAddress;Database=myDataBase; Uid=myUsername;Pwd=myPassword;

这样的连接字符串,可以用于ODBC连接方式,用 SqlConnection就不行了。

#8


你可以用vs自带的数据库连接控件来测试你的连接字符串;
测好后直接拷贝就行了

#9


引用 1 楼 computerfox 的回复:
你看看出错时,connectionString变量的值是什么?

connectionString=BoteAutoWMS.SQLServerDAL