我正在开发一个具有登录页面的Windows应用程序,用户在其中输入sql连接凭据用户名和密码。然后他们可以将数据从excel表上传到db。当用户按下运行按钮时,我遇到的问题是启动打开文件和读取数据的过程,而不是最终将值传递给DB,当它尝试连接到数据库时失败,因为他为用户名返回null密码。如何在将程序传递给数据库连接部分时让程序记住这些值?
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 System.IO;
using System.Threading;
using System.Globalization;
using System.Data.SqlClient;
namespace NewTestDataReader
{
public partial class DataUploader : Form
{
public DataUploader()
{
InitializeComponent();
}
internal string m_susername;
public string username { get { return this.m_susername; } set { this.m_susername = value; } }
internal string m_spassword;
public string password { get { return this.m_spassword; } set { this.m_spassword = value; } }
private void RunButton_Click(object sender, EventArgs e)
{
Thread oThread = new Thread(new ThreadStart(scan));
oThread.Start();
password = userPasswordInputTextBox.Text;
username = userNameInputTextBox.Text;
}
private void scan()
{
///scans the file and goes to FileReader function in separate TestDataReader.cs file
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.IO;
using System.Text.RegularExpressions;
using Excel = Microsoft.Office.Interop.Excel;
namespace TestDataReader
{
public class SheetOneDataReader : DataUploader
{
//parameters it reads from the excel sheet
internal string m_sName;
public string Name { get { return m_sName; } set { m_sName = value; } }
internal string m_sAddress;
public string Address{ get { return m_sAddress; } set { m_sAddress = value; } }
public void SaveSheetOneDataToDB()
{
SqlCommand cmd = new SqlCommand("UL_ExcelFirstSheetData");
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@Name", SqlDbType.NVarChar, 50).Value = this.Name;
cmd.Parameters.Add("@Address", SqlDbType.NVarChar, 50).Value = this.Address;
String connectionString;
SqlConnection cnn;
connectionString = "data source = 10.5.200.300";
connectionString += ";initial catalog = Excel_data" + ";Persist Security Info=True";
**connectionString += ";user id=" + username; //returns null
connectionString += ";password=" + password; //returns null**
cnn = new SqlConnection(connectionString);
try
{
cnn.Open();
}
catch
{}
}
}
public class FileReader
{
public static bool ReadFile(FileInfo oInfo)
{
try
{
//goes to SheetOneDataReader on top
SheetOneDataReader oDataSheetOne = new SheetOneDataReader();
oDataSheetOne.Name = //values from excel sheet
oDataSheetOne.Address = //values from the excel sheet
oDataSheetOne.SaveSheetOneDataToDB();
}
}
}
}
答案 0 :(得分:0)
username
和password
属性属于您在DataUploader
中继承的班级SheetOneDataReader
。您可以在RunButton_Click
下的DataUploader
中设置这些属性的值。当你继承那个类时,它将使用null重新初始化这两个属性,而不是保留你在其他类中指定的值。
而是为SheetOneDataReader
类中的以下属性设置值,这是您需要它们连接到数据库的位置。
password = userPasswordInputTextBox.Text;
username = userNameInputTextBox.Text;
或者您可能不得不采用其他一些州管理方式,例如Session
或ViewState