在 System.IO.IOException 中第一次偶然出现的“mscorlib.dll”类型的异常

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.IsolatedStorage;
using System.IO;
using System.Data.SqlClient;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

string name = "";

private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=localhost; Initial Catalog = StuManager;Integrated Security=true ";
try
{
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;

cmd.CommandText = "select count(*) from admin where name=@sname and password=@pwd";

//第一个参数
SqlParameter para1 = new SqlParameter();
para1.ParameterName = "@sname";
para1.SqlDbType = SqlDbType.VarChar;
para1.Value = txtname.Text;

//第二个参数
SqlParameter para2 = new SqlParameter();
para2.ParameterName = "@pwd";
para2.SqlDbType = SqlDbType.VarChar;
para2.Value = txtpwd.Text;

cmd.Parameters.Add(para1);
cmd.Parameters.Add(para2);

int i = Convert.ToInt32(cmd.ExecuteScalar());

if (i > 0)
{

// *****应该是从这个地方出错了,但为什么就不知道了,希望大神帮助啊******
File.Create("D:\\date\\" + txtname.Text + ".txt");
name = @"D:\date\" + txtname.Text + ".txt";
File.WriteAllText(name, txtname.Text);

//********我个是我想实现记录密码的功能的程序,我把它从项目中抽了出来,新建的个我还简单项目,但不知为什么一运行就说:“连接失败”,输出提示:“在 System.IO.IOException 中第一次偶然出现的“mscorlib.dll”类型的异常”,大神帮帮忙吧!!!
Form2 A = new Form2();
A.Show();
}
else
{
txtname.Text = "";
txtpwd.Text = "";
}
}
catch (Exception ex)
{
MessageBox.Show("连接失败");
txtpwd.Text = "";
txtname.Text = "";
txtname.Focus();
}
}
}
}

File.Create("D:\\date\\" + txtname.Text + ".txt"); //你在这里创建了一个文件
name = @"D:\date\" + txtname.Text + ".txt";
File.WriteAllText(name, txtname.Text); //这个方法会自动创建一个文件,与上面冲突了。删除上面的create方法
温馨提示:答案为网友推荐,仅供参考
相似回答