c# - 使用 C# 将记录插入 SQL Server 2012

标签 c# sql-server visual-studio-2010

我正在尝试使用 C# 将记录插入到 SQL Server 2012。

我是这样实现的,问题是insert record语句没有在表尾添加记录。

它工作正常,但有时(每次不同)它插入记录中间,有时工作正常。我不清楚为什么会这样。 Figure

任何灵魂?

public partial class AddDoc : System.Web.UI.Page
{

SqlConnection con = new SqlConnection(@"Data Source=.\BAKRSQL; Database=Diseases Prediction Sys;Integrated Security=True");

protected void Page_Load(object sender, EventArgs e)
{
    SqlCommand cmd;
    string com = "select top 1 DId From Doctor ORDER BY DId Desc;";
    con.Open();
    cmd = new SqlCommand(com, con);
    object count = cmd.ExecuteScalar();
    if (count != null)
    {
        int i = Convert.ToInt32(count);
        i++;
        TextBox1.Text = i.ToString();
    }
    else
    {
        TextBox1.Text = "101";
    }
    con.Close();
}

protected void Button2_Click(object sender, EventArgs) 
{    SqlConnection con = new SqlConnection();
        con.ConnectionString =@"Data Source=.\BAKRSQL; Database=Diseases Prediction Sys;Integrated Security=True";
        con.Open();
        SqlCommand cmd = new SqlCommand("insert into Doctor(DId,Name,Address,Mobile) values (@DId,@Name,@Address,@Mobile);", con);
        cmd.Parameters.AddWithValue("@DId", TextBox1.Text);
        cmd.Parameters.AddWithValue("@Name", TextBox2.Text);
        cmd.Parameters.AddWithValue("@Address", TextBox3.Text);
        cmd.Parameters.AddWithValue("@Mobile", TextBox4.Text);
        cmd.ExecuteReader();
        con.Close();
        con.Open();
}
}

最佳答案

表格没有顺序。简单的。您使用 ID 插入 - 但这只是一个字段。

你想要订单,你用订单声明选择。

编写的每个 SQL 数据库的基础。

关于c# - 使用 C# 将记录插入 SQL Server 2012,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35557104/

相关文章:

c# - 在 C# 中查询字典的最佳方式

c# - c# 邮件中的西里尔主题编码

c# - 托管环境中的临时内存

vb.net - 全屏运行,没有开始菜单

c++ - 通过摄像头进行人脸检测

c# - 两个数相除

sql-server - SQL Server 2005 中 CTE 的范围

SQL Server 2012 查询被 LCK_M_IS 阻止

sql - 在 where 子句中过滤右连接数据

c++ - CMFCButton.SetToolTip() 崩溃