使用插入存储过程与SQL Server

时间:2016-03-20 23:53:13

标签: c# sql-server winforms

当我最初学习编程时,我被告知有3种可能的结果;有一个错误,它有效,或什么也没发生。

我现在遇到了最后一个问题,我没有提交错误消息。

这是Windows窗体:

screenshot of windows form

按钮的代码如下。

using System;
using System.Collections.Generic;
using System.ComponentModel;

using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;

namespace Basque
{
    public partial class Form1 : Form
    {
        private String strFirstName;
        private String strLastName;

        public Form1()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            strFirstName = txtFirstName.Text;
            strLastName = txtLastName.Text;
            int tempNo;
            AddNames(strFirstName, strLastName);
            txtFirstName.Clear();
            txtLastName.Clear();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }

        public void AddNames(String strFirstName, String strLastName)
        {
            String connString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\BasqueNames.mdf;Integrated Security=True";

            SqlConnection oCON = new SqlConnection(connString);
            oCON.Open();

            SqlCommand oCMD = new SqlCommand();
            oCMD.Connection = oCON;
            oCMD.CommandType = CommandType.StoredProcedure;
            oCMD.CommandText = "usp_BasqueNames_Insert";

            oCMD.Parameters.AddWithValue("@First", strFirstName);
            oCMD.Parameters.AddWithValue("@Last", strLastName);

            oCMD.ExecuteNonQuery();
            oCON.Close();
        }
    }
}

数据库的架构如下

CREATE TABLE [dbo].[Names] 
(
    [Id]        INT        IDENTITY (1, 1) NOT NULL,
    [FirstName] NCHAR (10) NULL,
    [LastName]  NCHAR (10) NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC)
);

存储过程如下

CREATE PROCEDURE usp_BasqueNames_Insert
    @First NCHAR(10),
    @Last NCHAR(10)
AS
    INSERT INTO Names(FirstName, LastName)
    VALUES (@First, @Last)

    RETURN @@identity

令我感到奇怪的是,当我开始调试应用程序时,服务器资源管理器中的数据库名称会立即显示一个红色的x。

2 个答案:

答案 0 :(得分:0)

你能试试吗?并且请确保您尝试传递的值不超过10个字符,因为您的nchar只是(10);

private void btnSave_Click(object sender, EventArgs e)
        {
            AddNames(txtFirstname.Text, txtLastName.Text);
            txtFirstName.Clear();
            txtLastName.Clear();
        }


        public void AddNames(string strFirstName, string strLastName)
        {
            String connString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\BasqueNames.mdf;Integrated Security=True";

            using(SqlConnection oCON = new SqlConnection(connString))
           {
            SqlCommand oCMD = new SqlCommand("usp_BasqueNames_Insert",oCon);
    oCMD.CommandType = CommandType.StoredProcedure;

            oCMD.Parameters.Add("@First", SqlDbType.Nchar).Value = strFirstName;
            oCMD.Parameters.Add("@Last", SqlDbType.Nchar).Value = strLastName;

            oCMD.ExecuteNonQuery();
            }
        }

答案 1 :(得分:0)

感谢所有的建议和支持。

有一件事需要改变,我必须安装并连接到sql express。我发现Visual Studio数据库在Visual Studio中不可靠以连接。

override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.rowHeight = 44;
}