![](/img/trans.png)
[英]Unable to access SQL Server from WCF service (Windows authenticated) from client (console application) in C#
[英]Unable to access functions from another class (C# Windows Form, SQL Server Management Studio)
我正在使用 Visual Studio 中的 C# Windows Form Application 和 SQL Server Management Studio 學習數據庫連接。
我遇到的問題是我無法從SQLConn.cs Class 訪問AddParams()和sqlQueryCmd()函數。 我嘗試將訪問修飾符從私有更改為公共,但沒有奏效。 有人可以在這里指導我嗎? 我不知道我做錯了什么。
SQLConn.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using Microsoft.Data.SqlClient;
namespace dbConnectivityTest1
{
public class SQLConn
{
public static void Main1(string[] args)
{
using (SqlConnection connString = new SqlConnection("Server=ServerName;Database=DatabaseName;User Id=UserID;\r\nPassword=Password;"))
{
List<SqlParameter> paramList = new List<SqlParameter>();
string AddParams(string tableName, object insVal)
{
SqlParameter sqlparams = new SqlParameter(tableName, insVal);
paramList.Add(sqlparams);
}
SqlCommand sqlCmd = new SqlCommand();
string sqlQueryCmd(string sqlQuery)
{
int recordCount = 0;
string excptnShow = "";
try
{
connString.Open();
SqlCommand sqlCmd = new SqlCommand(sqlQuery, connString);
paramList.ForEach(p => { sqlCmd.Parameters.Add(p); });
paramList.Clear();
DataTable sqlDT = new DataTable();
SqlDataAdapter sqlDA = new SqlDataAdapter(sqlCmd);
recordCount = sqlDA.Fill(sqlDT);
}
catch(Exception ex)
{
excptnShow = "Error: \r\n" + ex.Message;
}
finally
{
if (connString.State == ConnectionState.Open)
{
connString.Close();
}
}
}
}
}
}
}
Form1.cs
namespace dbConnectivityTest1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private SQLConn sqlDB = new SQLConn();
private void enterNametextBox_Click(object sender, EventArgs e)
{
enterNametextBox.Clear();
}
private void submitButton_Click(object sender, EventArgs e)
{
while(fullNameTextBox.Text.Length <= 0)
{
MessageBox.Show("Please enter your Full Name!", "Caption", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
}
while(contactTextBox.Text.Length <= 0)
{
MessageBox.Show("Please enter your Contact Number!", "Caption", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
}
while(ageTextBox.Text.Length <= 0)
{
MessageBox.Show("Please enter your Age!", "Caption", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
}
while(emailTextBox.Text.Length <= 0)
{
MessageBox.Show("Please enter your E-mail!", "Caption", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
}
System.Text.RegularExpressions.Regex emailRegex = new System.Text.RegularExpressions.Regex(@"^[a-zA-Z0-9+_.-]+@[a-zA-Z0-9.-]+$");
while(!emailRegex.IsMatch(emailTextBox.Text))
{
MessageBox.Show("Please enter a valid E-mail!", "Caption", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
}
InsVal();
}
public void InsVal()
{
**I am trying to call the SQLConn.cs Functions here**
}
}
}
正如評論中的人所說,您必須將功能移出 Main1。
sing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using Microsoft.Data.SqlClient;
namespace dbConnectivityTest1
{
public class SQLConn
{
public string AddParams(string tableName, object insVal)
{
SqlParameter sqlparams = new SqlParameter(tableName, insVal);
paramList.Add(sqlparams);
}
public string sqlQueryCmd(string sqlQuery)
{
int recordCount = 0;
string excptnShow = "";
try
{
connString.Open();
SqlCommand sqlCmd = new SqlCommand(sqlQuery, connString)
paramList.ForEach(p => { sqlCmd.Parameters.Add(p); });
paramList.Clear();
DataTable sqlDT = new DataTable();
SqlDataAdapter sqlDA = new SqlDataAdapter(sqlCmd);
recordCount = sqlDA.Fill(sqlDT);
}
catch(Exception ex)
{
excptnShow = "Error: \r\n" + ex.Message;
}
finally
{
if (connString.State == ConnectionState.Open)
{
connString.Close();
}
}
}
public static void Main1(string[] args)
{
using (SqlConnection connString = new SqlConnection("Server=ServerName;Database=DatabaseName;User Id=UserID;\r\nPassword=Password;"))
{
List<SqlParameter> paramList = new List<SqlParameter>();
SqlCommand sqlCmd = new SqlCommand();
}
}
}
}
Movint addParam 方法/函數無法解決問題,因為 SqlParam 變量在 sqlConn class 中不可用。 實現目標的更好方法是:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
namespace dbConnectivityTest1
{
public class SQLConn
{
private readonly string _connectionString;
private List<SqlParameter> _paramList;
public SQLConn()
{
_connectionString="Server=ServerName;Database=DatabaseName;User Id=UserID;\r\nPassword=Password;";
_paramList = new List<SqlParameter>();
}
public void AddParams(string tableName, object insVal)
{
SqlParameter sqlparams = new SqlParameter(tableName, insVal);
_paramList.Add(sqlparams);
}
public void sqlQueryCmd(string sqlQuery)
{
using (var sqlConn = new SqlConnection(_connectionString))
{
int recordCount = 0;
string excptnShow = "";
try
{
sqlConn.Open();
SqlCommand sqlCmd = new SqlCommand(sqlQuery, sqlConn);
_paramList.ForEach(p => { sqlCmd.Parameters.Add(p); });
_paramList.Clear();
DataTable sqlDT = new DataTable();
SqlDataAdapter sqlDA = new SqlDataAdapter(sqlCmd);
recordCount = sqlDA.Fill(sqlDT);
}
catch (Exception ex)
{
excptnShow = "Error: \r\n" + ex.Message;
}
finally
{
if (sqlConn.State == ConnectionState.Open)
{
sqlConn.Close();
}
}
}
}
}
}
在這種情況下,不需要 Main 方法,因為每個應用程序只能有一個 Main 方法,而 winForms 默認情況下已經有一個 Main 方法。
其次,SQLConn class 可以具有 List 類型的私有字段,以及一個名為 AddParam 的公共方法,其職責是向其添加新參數。
最后,添加了一個公共 SqlQueryCmd 方法來執行作業的 rest。
您現在可以使用此 class 執行您的查詢,如下所示:
private void button1_Click(object sender, EventArgs e)
{
var sqlConn = new SQLConn();
sqlConn.AddParams("tableName","your Object");
sqlConn.AddParams("tableName","your Object");
sqlConn.AddParams("tableName","your Object");
sqlConn.sqlQueryCmd("Query");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.