本地程序运行未在Windows Server 2008 R2上运行

时间:2014-04-04 08:21:12

标签: c# database sql-server-2008

大家好我已经用c#编写了一个程序。代码在我当地的maschin上使用mssql express。程序没有错,它正在运行。但是当我在Windows Server 2008 R2上加载构建时,程序将无法运行。 所以这个程序非常庞大而复杂,并且有很多类,所以我决定编写一个simpel程序来测试。 我有同样的问题

这是测试的测试范例代码,它是简单的,只与数据库建立连接并在其中写入hallo。

Ausol程序是signet。在服务器上安装了net 4.5。 所以我不知道为什么这不起作用

代码#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Data.Sql;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Windows;
using System.Windows.Shapes;

namespace WpfApplication2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
public partial class MainWindow : Window
{
    SqlConnection DBConnection = null;
    public MainWindow()
    {
        InitializeComponent();
        DBConnection = new SqlConnection(GetConnectionString());
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("Hei ho");
        string Wort = "Hallo";
        string Name = Insersql(Wort);
        MessageBox.Show(Name);

    }

    //SQL open&insert
    private string Insersql(string Ausruf)
    {    
        try
        {
            DBConnection.Open();
            string Query = "insert into It_Infrastruktur (ITname) values('" + Ausruf + "')"; // speichern des ITelementnamen
            SqlCommand createCommand = new SqlCommand(Query, DBConnection);
            createCommand.ExecuteNonQuery();
            DBConnection.Close();  
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return Ausruf;
    } 

    // SQL connection
    public void OpenSqlConnection()
    {
        string connectionString = GetConnectionString();

        using (SqlConnection connection = new SqlConnection(GetConnectionString()))
        {
            connection.ConnectionString = connectionString;
            connection.Open();
            Console.WriteLine("State: {0}", connection.State);
            Console.WriteLine("ConnectionString: {0}",
                connection.ConnectionString);
        }
    }

    private static string GetConnectionString()
    {


        return @"Data Source=(local);Initial Catalog=It_Infrastruktur;"
            + "Integrated Security=SSPI;";
        //return @"Data Source=(local)\SQLEXPRESS;Initial Catalog=It_Infrastruktur;"
        //    + "Integrated Security=SSPI;";
    }


 }
}

XAML代码

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button Content="Button" HorizontalAlignment="Left" Margin="178,120,0,0"     VerticalAlignment="Top" Width="75" Click="Button_Click"/>

    </Grid>
</Window>

sql脚本

create table It_Infrastruktur (
   ElementID                 bigint                        identity,
   ITname                    varchar(255)                  null,


   constraint PK_It_Infrastruktur primary key  (ElementID)
)
go

create table Netzwerkelement (
   Bezeichnung               varchar(255)                  null,
   IPadresse                 varchar(255)                  null,
   ITtype                    varchar(255)                  null,
   ITart                     varchar(255)                  null,
   Aartesys_artikelNr        varchar(255)                  null,
   Erwerbsdatum              varchar(255)                  null,
   Hersteller                varchar(255)                  null,
   Administrator             varchar(255)                  null,
   PW                        varchar(255)                  null,
   Port_anzahl               varchar(255)                  null,
   Hostip                    varchar(255)                  null,
   Beschreibung              varchar(MAX)                  null,
   Mac_Adresse               varchar(255)                  null,
   ElementID                 bigint                            null,

)
go

create table Logger (
   Mitarbeiter   varchar(225) null, 
   LogInfo      varchar(MAX)   null,
   LogDatum     DATETIME   null,

)
go  

thx求助

所以我写了一个sibel conol输出

它看起来像右边的问题 但我不明白为什么因为我有这个数据库的管理权我可以选择前1000行所有这一切

      Hello world
xxx\Desktop>"Hello world.exe"
Hello worldSystem.Data.SqlClient.SqlException (0x80131904): Cannot open database
 "It_Infrastruktur" requested by the login. The login failed.
Login failed for user 'xxx'.
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception
, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObj
ect stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand
 cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler,
TdsParserStateObject stateObj, Boolean& dataReady)
   at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cm
dHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, Tds
ParserStateObject stateObj)
   at System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlis
tOK)
   at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo
serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSn
iOpenTimeout, TimeoutTimer timeout, Boolean withFailover)
   at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo
serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirect
edUserInstance, SqlConnectionString connectionOptions, SqlCredential credential,
 TimeoutTimer timeout)
   at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTime
r timeout, SqlConnectionString connectionOptions, SqlCredential credential, Stri
ng newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance)
   at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdent
ity identity, SqlConnectionString connectionOptions, SqlCredential credential, O
bject providerInfo, String newPassword, SecureString newSecurePassword, Boolean
redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData r
econnectSessionData)
   at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOp
tions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConn
ectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)

   at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConn
ectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConne
ctionPoolKey poolKey, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owning
Object, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
   at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection o
wningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection
)
   at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection ow
ningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean o
nlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& co
nnection)
   at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection ow
ningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbCon
nectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection
 owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions
, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(Db
Connection outerConnection, DbConnectionFactory connectionFactory, TaskCompletio
nSource`1 retry, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection
 outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1
retry, DbConnectionOptions userOptions)
   at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 re
try)
   at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
   at System.Data.SqlClient.SqlConnection.Open()
   at Hello_world.Program.Main(String[] args)
ClientConnectionId:402532f5-2b15-4075-87a9-b61ac0b0fd7b
Unhandled Exception: System.Data.SqlClient.SqlException: Cannot open database "I
t_Infrastruktur" requested by the login. The login failed.
Login failed for user 'xxx'.
   at Hello_world.Program.Main(String[] args)

1 个答案:

答案 0 :(得分:0)

确保SQL数据库接受Windows集成身份验证,并且本地计算机用户已定义且具有权限。