小编use*_*203的帖子

使用sql查询结果填充datagridview

我正在尝试提供查询结果,但我一直在获取空白数据网格.这就像数据本身不可见

这是我的代码:

 private void Employee_Report_Load(object sender, EventArgs e)
 {
     string select = "SELECT * FROM tblEmployee";
     Connection c = new Connection();
     SqlDataAdapter dataAdapter = new SqlDataAdapter(select, c.con); //c.con is the connection string
     SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);

     DataTable table = new DataTable();
     table.Locale = System.Globalization.CultureInfo.InvariantCulture;
     dataAdapter.Fill(table);
     bindingSource1.DataSource = table;

     dataGridView1.ReadOnly = true;        
     dataGridView1.DataSource = bindingSource1;
}
Run Code Online (Sandbox Code Playgroud)

这段代码出了什么问题?

c# datagridview winforms

26
推荐指数
2
解决办法
16万
查看次数

在具有节点和边缘权重的图形中找到最短路径?

假设我有一个加权图,在边和顶点上都有权重。如何找到从某个节点s到某个节点t的“最便宜”路径?我的复杂度应为O(n 2(n + m))。

language-agnostic algorithm performance complexity-theory graph

4
推荐指数
1
解决办法
1865
查看次数

C#Linq返回对象

我正在尝试实现一个方法:

  public Referee GetRefereeById(int refereeId)
        {
            var result = from b in Referees
                         where b.PersonId.Equals(refereeId)
                         select b;
            return (Referee)result;
        }
Run Code Online (Sandbox Code Playgroud)

该方法应该返回一个裁判对象.知道我做错了什么吗?

c# linq

4
推荐指数
1
解决办法
2502
查看次数

连接到SQL Server时出现"路径中的非法字符"错误

我正在尝试连接到数据库,我收到以下错误:

路径中的非法字符.

这是我的代码:

private void button1_Click(object sender, EventArgs e)
{
    SqlConnection Con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\targil3.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
    SqlDataAdapter adapt = new SqlDataAdapter();
    adapt.InsertCommand = new SqlCommand(" INSERT INTO tblEmployee VALUES (@employeeNumber, @employeePrivateName, @employeeFamilyName ,@city, @street, @houseNo, @phoneNumber, @birthDate, @startWorkingDate)", Con);
    adapt.InsertCommand.Parameters.Add("@employeeNumber", SqlDbType.Char).Value = textBox1.Text;
    adapt.InsertCommand.Parameters.Add("@employeePrivateName", SqlDbType.VarChar).Value = textBox2.Text;
    adapt.InsertCommand.Parameters.Add("@employeeFamilyName", SqlDbType.VarChar).Value = textBox3.Text;
    adapt.InsertCommand.Parameters.Add("@city", SqlDbType.VarChar).Value = textBox4.Text;
    adapt.InsertCommand.Parameters.Add("@street", SqlDbType.VarChar).Value = textBox5.Text;
    adapt.InsertCommand.Parameters.Add("@houseNo", SqlDbType.Int).Value = textBox6.Text;
    adapt.InsertCommand.Parameters.Add("@phoneNumber", SqlDbType.Char).Value = textBox7.Text;
    adapt.InsertCommand.Parameters.Add("@birthDate", SqlDbType.DateTime).Value = Convert.ToDateTime(textBox8.Text);
    adapt.InsertCommand.Parameters.Add("@startWorkingDate", SqlDbType.DateTime).Value = Convert.ToDateTime(textBox8.Text);


    Con.Open();
    adapt.InsertCommand.ExecuteNonQuery(); …
Run Code Online (Sandbox Code Playgroud)

.net c# sql database sql-server

2
推荐指数
1
解决办法
2622
查看次数

错误 - 尝试连接到sql server数据库时"找不到服务器或无法访问服务器"

我正在研究本地项目并尝试连接到本地数据库.我有这个类的连接字符串:

public class SQLConnection
{
    public static string connectionString =
        @"Data Source=TZVIKA-PC\SQLEXPRESS;Initial Catalog=WorldCup;Integrated Security=True";

    public static WorldCupDBDataContext wcDataContext = 
        new WorldCupDBDataContext(connectionString);

    public static WorldCupDBDataContext GetDataContextInstance()
    {
        return wcDataContext;
    }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试在Windows窗体中执行此行时:

 teamBindingSource.DataSource = database.Teams.OrderBy(team => team.TeamId);
Run Code Online (Sandbox Code Playgroud)

我得到以下sql异常:

建立与SQL Server的连接时发生与网络相关或特定于实例的错误.服务器未找到或无法访问.验证实例名称是否正确,以及SQL Server是否配置为允许远程连接.(提供程序:SQL网络接口,错误:26 - 查找指定的服务器/实例时出错)

我的朋友运行了代码,它可以在他的电脑上运行(当然将连接字符串更改为相关的字符串时)

为什么它不适合我?

c# sql-server

1
推荐指数
1
解决办法
7317
查看次数