在Visual Studio中使用SQL Server存储过程

Clo*_*chy 1 c# sql stored-procedures sql-server-2005 visual-studio-2010

我的经历和我正在使用的

所以我刚开始了与ASP.NET中的一个非常基本的Web应用程序,以获得与SQL Server管理工作室和视觉工作室2010年.通常情况下多了几分熟悉,我使用MySQL,PHP和崇高的文本编辑器2.我没有C#经验,并在Visual Studios中实现数据库.所以我正在尝试使用SQL Server Management Studios中的存储过程并在Visual Studios 2010中实现它.

问题

所以这是我的问题:我正在尝试创建一个链接到SQL Server的基本网页,并能够添加,删除,搜索和显示数据库中的所有记录.现在我根据我认为正确添加/删除的内容编写了自己的代码,单击按钮时没有任何反应.所以我相信你可以看到我的沮丧来自哪里.我不确定问题是在我的C#编码还是我的SQL编码中.

我想专注于让我的添加/删除按钮工作,然后找出显示所有文件的逻辑.我希望能够单击一个按钮,然后让它显示所有文件而不是仅显示网格.我的数据库名为FirstApp.

这是我的web.config文件中的内容:

<add name="FirstApp" connectionString="Data Source=PCNAME\SQLEXPRESS;Initial Catalog=FirstApp;Integrated Security=True"
  providerName="System.Data.SqlClient" />
Run Code Online (Sandbox Code Playgroud)

现在这是我的Default.aspx.cs文件中的内容:

*现在正确的代码!*

  namespace FirstApp
  {
  public partial class _Default : System.Web.UI.Page
  {
  public string CommandArgument { get; set; }
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    private void MessageBox(string msg)
    {
        Label lbl = new Label();
        lbl.Text = "<script language='javascript'>" + Environment.NewLine +  "window.alert('" + msg + "')</script>";
        Page.Controls.Add(lbl);
    }

    //Add a new company to the database
    protected void add_Click(object sender, EventArgs e)
    {
        SqlDataReader rdr = null;
        string connectionString = null;
        SqlConnection cnn;
        connectionString = "Data Source=ITXDK29M91\\SQLEXPRESS;Initial Catalog=FirstApp;Integrated Security=True";
        cnn = new SqlConnection(connectionString);
        try
        {
            cnn.Open();
            SqlCommand cmd = new SqlCommand("dbo.Add_Company", cnn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@companyname", companyname.Text);
            cmd.Parameters.AddWithValue("@companyphone", companyphone.Text);
            cmd.Parameters.AddWithValue("@companyid", companyid.Text);
            cmd.Parameters.AddWithValue("@companytype", companytype.Text);
            rdr = cmd.ExecuteReader();
         }
        finally
        {

            //Close the connections 
            if (cnn != null)
            {
                cnn.Close();
            }
            if (rdr != null)
            {
                rdr.Close();
            }
        }
     }

     //Delete a company from the database
     protected void delete_Click(object sender, EventArgs e)
     {
         SqlDataReader rdr = null;
        SqlConnection cnn;
        string connectionString = null;
        connectionString = "Data Source=ITXDK29M91\\SQLEXPRESS;Initial Catalog=FirstApp;Integrated Security=True";
        cnn = new SqlConnection(connectionString);
        try
        {
            cnn.Open();
            SqlCommand cmd = new SqlCommand("dbo.deleteCo", cnn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@ID", SqlDbType.Int);
            rdr = cmd.ExecuteReader();

        }

        finally
        {

            //Close the connections 
            if (cnn != null)
            {
                cnn.Close();
            }
            if (rdr != null)
            {
                rdr.Close();
            }
        }
    }


    protected void Search_Click(object sender, EventArgs e)
    {

    }

    protected void Getall_Click(object sender, EventArgs e)
    {

    }

  }
  }
Run Code Online (Sandbox Code Playgroud)

这是我在Default.aspx中的源代码中的内容

 <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">      <h2>Ready for an Adventure? Let&#39;s get started! 
     </h2> <hr />This is where you can enter information about your company. 
 <br />
 <form method="post" action="">
Company Name:<br /> 
<asp:TextBox ID="companyname" runat="server"></asp:TextBox>
<br />
Company Phone Number:<br />
<asp:TextBox ID="companyphone" runat="server"></asp:TextBox>
<br />
Company Tax ID Number:
<br />
<asp:TextBox ID="companyid" runat="server"></asp:TextBox>
<br />
Type of business: <br />
     <asp:TextBox ID="companytype" runat="server"></asp:TextBox>
<br />
 <asp:Button ID="add" runat="server" BackColor="DeepSkyBlue" 
BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" 
CssClass="submitButton" Font-Names="Palatino Linotype" ForeColor="White" 
onclick="add_Click" Text="Submit" Width="128px" />
</form> <hr /> 
Want to delete your company information?<br />
Enter in the Company ID Number:&nbsp;
<br />
 <asp:TextBox ID="PrimaryKey" runat="server" Width="120px"></asp:TextBox>
 <br />
 <asp:Button ID="delete" runat="server" BackColor="DeepSkyBlue" 
BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" 
CssClass="submitButton" Font-Names="Palatino Linotype" ForeColor="White" 
onclick="delete_Click" Text="Delete Info" Width="119px" />
<br />
<hr /> 
Looking for similar companies?
<br />
(Ex: Retail, Designer, Restaurant, etc.) &nbsp;
<br />
Enter the type of company:
 <br />
 <asp:TextBox ID="scompanyid" runat="server" Width="120px"></asp:TextBox>
 <br />
 <asp:Button ID="Search" runat="server" BackColor="DeepSkyBlue" 
 BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" 
 CssClass="submitButton" Font-Names="Palatino Linotype" ForeColor="White" 
 onclick="Search_Click" Text="Start Searching!" Width="119px" />
 <br />
 <hr /> 
   Want to see all the companies that we work with? <br /> 
   Click the button below! 
 <br />
 <asp:Button ID="Getall" runat="server" BackColor="DeepSkyBlue" 
 BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" 
 CssClass="submitButton" Font-Names="Palatino Linotype" ForeColor="White" 
 onclick="Getall_Click" Text="Get all records!" Width="119px" />
 <br />
   <br />
   </asp:Content>
Run Code Online (Sandbox Code Playgroud)

更新:我已更新代码以显示正确的代码.添加按钮有效,但我的删除按钮不起作用.我还在努力想出那一个.

Joe*_*nos 6

您实际上并没有打开连接或执行SQL命令.通常,执行简单命令的方式是:

using (var conn = new SqlConnection(connectionString))
{
    using (var comm = conn.CreateCommand())
    {
        conn.Open();
        comm.CommandText = "SOME SQL HERE";

        // command type, parameters, etc.

        //pick one of the following
        comm.ExecuteNonQuery();
        int value = (int)comm.ExecuteScalar();
        SqlDataReader reader = comm.ExecuteReader();

    }
}
Run Code Online (Sandbox Code Playgroud)

  • 啊!有人使用连接,我现在感觉好多了. (2认同)