C#Winform应用程序中的System.Data.SqlClient.Sql错误

时间:2013-03-04 13:03:42

标签: c# winforms sql-server-2005

当我尝试在客户端计算机上运行我的C#Winform应用程序时出现以下错误

Description:
  Stopped working

Problem signature:
  Problem Event Name:   CLR20r3
  Problem Signature 01: ics.exe
  Problem Signature 02: 1.0.0.0
  Problem Signature 03: 5134926a
  Problem Signature 04: System.Data
  Problem Signature 05: 2.0.0.0
  Problem Signature 06: 4a275e65
  Problem Signature 07: 2755
  Problem Signature 08: 29
  Problem Signature 09: System.Data.SqlClient.Sql
  OS Version:   6.1.7600.2.0.0.768.2
  Locale ID:    1033

Read our privacy statement online:
  http://go.microsoft.com/fwlink/?linkid=104288&clcid=0x0409

If the online privacy statement is not available, please read our privacy statement     offline:
  C:\Windows\system32\en-US\erofflps.txt

这里是Application.Run代码

    [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new frmLogin2());
        }

这是表格登录代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using ICS.Classes;
using System.Threading;
using Microsoft.Win32;

namespace ICS.Forms
{
    public partial class frmLogin2 : Office2007Form
    {
        static DataAccess da = new DataAccess();
        static DataTable dt = new DataTable();
        int num = 0;
        public frmLogin2()
        {
            Thread t = new Thread(new ThreadStart(SplashScreen));
        t.Start();
        Thread.Sleep(5000);
        InitializeComponent();
        t.Abort();
    }

    void Registries()
    {
        try
        {
            RegistryKey regKey;
            regKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\ICS\POS", true);
            string activated = EncDec.Decrypt(regKey.GetValue("Activated").ToString(), "A!11").ToString();
            if (activated != "TRUE")
            {
                string coder1;
                coder1 = regKey.GetValue("ICSPOS").ToString().Trim();
                num = int.Parse(EncDec.Decrypt(coder1, "A!11"));
                if (num < 30)
                {
                    num++;
                    regKey.SetValue("ICSPOS", EncDec.Encrypt(num.ToString(), "A!11"));
                    lblNum.Text += (30 - num).ToString();
                }
            }
        }
        catch (Exception ex)
        {
            string coder1 = "";
            try
            {
                RegistryKey regKey;
                regKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\ICS\POS", true);
                coder1 = regKey.GetValue("ICSPOS").ToString().Trim();
            }
            catch
            {

            }
            RegistryKey creator;
            creator = Registry.LocalMachine.OpenSubKey("SOFTWARE", true);
            creator.CreateSubKey(@"ICS\POS");
            creator.Close();
            RegistryKey REG2;
            REG2 = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\ICS\POS", true);
            REG2.SetValue("AppName", "POS");
            REG2.SetValue("Version", 1.0);
            REG2.SetValue("Activated", EncDec.Encrypt("No", "A!11"));
            if (string.IsNullOrEmpty(coder1))
            {
                REG2.SetValue("ICSPOS", EncDec.Encrypt("1", "A!11"));
                lblNum.Text += (30 - 1).ToString();
            }
            REG2.Close();
        }
    }

    private void frmLogin2_Load(object sender, EventArgs e)
    {
        try
        {
            ICS_Auth aut = new ICS_Auth();
            Registries();
            //MessageBox.Show(aut.CreatSerial());
            if (num == 1)
            {
                da.ExecuteNonQuery("sp_admin_user");
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    private void btnLogin_Click(object sender, EventArgs e)
    {
        dt = da.GetDataTable("User_Login", da.CreateSqlParamter("@User_Name", txtUsername.Text)
                                       , da.CreateSqlParamter("@User_Pass", txtUserpass.Text));
        if (dt.Rows.Count == 1)
        {
            //this.Close();
            frmMain main = new frmMain(dt);
            main.Show();
            this.Hide();
        }
        else
        {
            MessageBox.Show("اسم المستخدم او كلمة المرور غير صحيحة");
        }
    }

    private void btnClose_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    public void SplashScreen()
    {
        Application.Run(new frmSplash());
    }

    public static DataTable LOGIN()
    {
        frmLogin2 log = new frmLogin2();
        log.ShowDialog();
        return dt;
    }

    private void frmLogin2_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == (char)27)this.Close();
    }
}
}

这是app.config中的连接字符串

<add key="ConnStr" value="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\POS.mdf;Integrated Security=True;User Instance=True"/>

我正在使用Visual Studio 2008和SQL Server 2005 Express

2 个答案:

答案 0 :(得分:2)

实际上,问题出在连接字符串中,而不是Data Source=部分。这是User Instance=True

除非您对其需求绝对肯定,否则我建议将其删除 - User Instance的默认值为False

我假设问题归结为权限/磁盘配额和其他与系统管理员相关的问题。 Here您可以找到有关User Instance是什么以及如何使用它的更多信息。

答案 1 :(得分:0)

您的数据源是问题所在。你现在有:

Data Source=.\SQLEXPRESS

但你需要的是(本地)这样的:

Data Source=(local)